Selenium Grid 并行测试不能并行工作

Selenium Grid Parallel Test Is Not Work Parallel

您好,我有两个节点,名称分别为 Node1Node2,我在一台计算机上进行此测试。我的问题是 Node1Node2 测试不能同时工作,所以它们不是并行的。因此,当 Node1Node2 测试开始后完成但我不想要它时,我希望他们开始 together.I 已经尝试了 parallel="tests"parallel="classes" TestNG.xml 文件。

这是我的 Node1.java 文件:

package grid;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class Node1 {
    WebDriver driver;
    String nodeUrl;
  @Test
  public void f() {
        try {
            //configuration
            nodeUrl= "http://192.168.56.1:5555/wd/hub";
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setBrowserName("chrome");
            capabilities.setPlatform(Platform.WIN10);
            driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);

            //test scripts
            driver.manage().deleteAllCookies();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
            driver.get("https://www.amazon.com/");
            driver.findElement(By.linkText("Today's Deals")).click();
            driver.findElement(By.linkText("Gift Cards")).click();
            driver.findElement(By.linkText("Today's Deals")).click();
            driver.findElement(By.linkText("Gift Cards")).click();
            driver.findElement(By.linkText("Today's Deals")).click();
        } 
        catch (MalformedURLException e) {       
            e.printStackTrace();            
        }
  }
}

这是我的 Node2.java 文件:

package grid;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Platform;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.Test;

public class Node2 {    
    WebDriver driver;
    String nodeUrl;
  @Test
  public void f() {
        try {
            //configuration
            nodeUrl= "http://192.168.56.1:5555/wd/hub";
            DesiredCapabilities capabilities = DesiredCapabilities.chrome();
            capabilities.setBrowserName("chrome");
            capabilities.setPlatform(Platform.WIN10);
            driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);

            //test scripts
            driver.manage().deleteAllCookies();
            driver.manage().window().maximize();
            driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
            driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
            driver.get("https://www.google.com/");
        } 
        catch (MalformedURLException e) {       
            e.printStackTrace();            
        }
  }
}

这是我的 TestNG.xml 文件:

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >

<suite name="Test Grid" parallel="tests">
    <test name="Test Node1">
        <classes>
            <class name="grid.Node1" />
        </classes>
    </test>
    <test name="Test Node2">
        <classes>
            <class name="grid.Node2" />
        </classes>
    </test>
</suite>

试试下面的 xml 代码。

<?xml version="1.0" encoding="UTF-8"?>
    <suite name="Test Grid" parallel="classes" thread-count="2" >
        <test name="Test ALl" preserve-order="false">
            <classes>
                <class name="grid.Node1" />
            </classes>
            <classes>
                <class name="grid.Node2" />
            </classes>
        </test>
    </suite>

试试下面的 testNG.xml` 对我有用的文件。

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite">
  <test name="Test" parallel="classes" thread-count="2">
    <classes>
      <class name="com.github.parallel.Parallel1"/>
      <class name="com.github.parallel.Parallel2"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

如果在两个测试中将 WebDriver 字段声明移动到 @Test 方法中。它应该也可以使用 parallel="methods"。字段不是线程安全的。

 @Test
public void f() {
    try {
        WebDriver driver;
        //configuration
        nodeUrl= "http://192.168.56.1:5555/wd/hub";
        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setBrowserName("chrome");
        capabilities.setPlatform(Platform.WIN10);
        driver = new RemoteWebDriver(new URL(nodeUrl), capabilities);

        //test scripts
        driver.manage().deleteAllCookies();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
        driver.get("https://www.google.com/");
    }
    catch (MalformedURLException e) {
        e.printStackTrace();
    }
}