[英]Can I use a single driver to run parallely multiple tests in Selenium (Java)?
With the way my project is structured, it was made with all functions calling one static driver, since I didn't expect I'd need to do many tests simultaneously.按照我的项目的结构方式,它是由调用一个静态驱动程序的所有函数组成的,因为我没想到我需要同时进行许多测试。 Now I'm using TestNG and, right now it's opening up multiple tests and doing both test actions in one of the windows, effectively crashing the test.
现在我正在使用 TestNG,并且现在它正在打开多个测试并在其中一个窗口中执行两个测试操作,从而有效地使测试崩溃。
This happens because all the basic functions that I've made (such as clickbutton, login user, etc) call a static driver from a class I've named DriverManager.发生这种情况是因为我所做的所有基本功能(例如单击按钮、登录用户等)都从我命名为 DriverManager 的类中调用静态驱动程序。 And to change that I'd have to basically refactor the entire code, putting a driver as an entry object to most functions I've made.
为了改变这一点,我必须基本上重构整个代码,将驱动程序作为我所做的大多数函数的入口对象。
Is there any way I can do the tests parallelly using TestNG with only one driver?有什么办法可以只使用一个驱动程序使用 TestNG 并行进行测试吗?
Yes you can do it ,
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "https://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="tests" thread-count="2">
<test name="Test">
<classes>
<class name="Parallel"/>
</classes>
</test> <!-- Test -->
</suite> <!-- Suite -->
This is XML file
Parallel is class name
You can do parallel by **method,tests,classes and instances**
following code for run test parallel
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;
public class Parallel {
WebDriver driver;
@Test
public void test1() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\User\\IdeaProjects\\SeleniumParallelTestingFinal\\src\\main\\resources\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Testing");
}
@Test
public void test2() {
System.setProperty("webdriver.gecko.driver","C:\\Users\\User\\IdeaProjects\\SeleniumParallelTestingFinal\\src\\main\\resources\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.google.com/");
driver.findElement(By.name("q")).sendKeys("Testing");
}
public static void main(String[] args) throws Exception {
System.out.println("WELCOME TO WORLD JESUS");
}
@AfterMethod
public void tearDown()
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.