简体   繁体   English

我可以使用单个驱动程序在 Selenium (Java) 中并行运行多个测试吗?

[英]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.

相关问题 如何实例化远程Web驱动程序,以便我可以在Java和Selenium中通过Serenity并行运行测试 - How to instantiate Remote Web Driver so I can run tests in parallel using Java and Selenium with Serenity 我可以在 java 的父选项卡和子选项卡中同时运行测试吗? (硒 TestNG) - Can I able to run the test parallely in both parent and child tab in java ? (selenium TestNG ) 从单个类运行多个硒测试(java) - Run multiple selenium tests from a single class(java) Selenium并行运行多个浏览器实例 - Selenium Run multiple browser instances parallely 我们如何在 Selenium Web 驱动程序中使用预填充 cookie 来更快地运行测试 - How can we use pre-populate cookies in Selenium Web driver to run the tests faster 如何在单个浏览器中运行多个UI测试? - How can I run multiple UI tests in a single browser? 如何使用 Object 迭代器 DataProvider 并在 java 中并行运行多个测试? - How can I use an Object iterator DataProvider and run multiple tests in parallel in java? 如何使用Selenium Java在同一台机器上并行运行方法或类 - how to run methods or classes parallely on same machine using selenium java 在Selenium Web驱动程序中并行执行sendkeys - perform sendkeys parallely in selenium web driver 使用相同的Firefox窗口在Selenium WebDriver(Java)中运行多个测试 - Using the same Firefox Window to run multiple tests in Selenium WebDriver (Java)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM