我在一个类中有两个测试用例。当使用TestNg并行运行时,它将所有测试数据连接起来,并放置在一个浏览器中,而另一个浏览器为空。
硒版本:3.8.21 Firefox:最新 测试:6.8.21
package cli;
import java.net.MalformedURLException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class test1 {
public WebDriver driver;
@BeforeMethod
public void setUp() throws MalformedURLException {
System.out.println("@BeforeMethod: The annotated method will be run before each test method.");
}
@Test
public void test() throws MalformedURLException, InterruptedException {
driver = jenkinHub.hub();
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Testing");
System.out.println("This is current Url " + driver.getCurrentUrl());
Thread.sleep(9000);
driver.quit();
}
@Test
public void NotTesting() throws MalformedURLException, InterruptedException {
driver = jenkinHub.hub();
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Not Testing");
System.out.println("This is current Url " + driver.getCurrentUrl());
Thread.sleep(9000);
driver.quit();
}
}
xmlFile
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Suite" verbose="1" parallel="methods" thread-count="3">
<test name="REgressionSuite" group-by-instances="true">
<classes>
<class name="cli.test1" />
</classes>
</test>
</suite>
答案 0 :(得分:0)
这里是如何并行执行示例,您可以根据需要更改驱动程序实例化方法。尝试此操作,然后在代码中进行相应的调整,它将打开两个镶边并在其中搜索测试项,并且不会串联。
package Grid
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
public class test1 {
DesiredCapabilities caps;
@BeforeMethod
public void setUp() throws MalformedURLException {
System.out.println("@BeforeMethod: The annotated method will be run before each test method.");
caps = new DesiredCapabilities();
caps = DesiredCapabilities.chrome();
}
@Test
public void testing() throws MalformedURLException, InterruptedException {
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Testing");
System.out.println("This is current Url " + driver.getCurrentUrl());
Thread.sleep(9000);
driver.quit();
}
@Test
public void NotTesting() throws MalformedURLException, InterruptedException {
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"),caps);
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Not Testing");
System.out.println("This is current Url " + driver.getCurrentUrl());
Thread.sleep(9000);
driver.quit();
}
}
testng.xml文件(与您编辑程序包名称相同)
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
<suite name="Regression Suite" verbose="1" parallel="methods" thread-count="4">
<test name="REgressionSuite">
<classes>
<class name="Grid.test1" />
</classes>
</test>
</suite>
答案 1 :(得分:0)
可能是因为您的driver
是班级级别的字段。当第一个测试方法将其初始化(driver = jenkinHub.hub()
)并尝试使用它时,另一种方法会为其重新分配新值,因此所有操作均使用相同的WebDriver
实例完成。
尝试以这种测试方法将其移动
@Test
public void test() throws MalformedURLException, InterruptedException {
WebDriver driver = jenkinHub.hub(); // create WebDriver instance
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Testing");
System.out.println("This is current Url " + driver.getCurrentUrl());
Thread.sleep(9000);
driver.quit();
}
@Test
public void NotTesting() throws MalformedURLException, InterruptedException {
WebDriver driver = jenkinHub.hub(); // create another WebDriver instance
driver.get("https://www.google.com/");
driver.findElement(By.xpath("//*[@id=\"lst-ib\"]")).sendKeys("Not Testing");
System.out.println("This is current Url " + driver.getCurrentUrl());
Thread.sleep(9000);
driver.quit();
}