微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TestNG 并行测试,打开两个浏览器,但只在 1 个浏览器中运行所有测试

如何解决TestNG 并行测试,打开两个浏览器,但只在 1 个浏览器中运行所有测试

当我运行此代码时,它首先打开两个浏览器 (Chrome) 并转到基本 URL。但对我来说,这是令人困惑的部分。当浏览器打开时,在这两个浏览器中,只有一个浏览器会收到测试命令。在我的例子中,我正在运行一个类,这个类有两个测试用例。但是所有的测试用例都将在一个浏览器中运行。第二个浏览器刚刚打开并转到基础网址

基础类:

    package com.cross.base;
    import io.github.bonigarcia.wdm.WebDriverManager;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.edge.EdgeDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.ie.InternetExplorerDriver;
    import org.openqa.selenium.support.ui.webdriverwait;
    import org.testng.annotations.Parameters;
    
    public class MultiThread_Class {
        public   static  webdriverwait driverWait;
        protected static ThreadLocal<WebDriver> driverThreadLocal = new ThreadLocal <>();
    
        @Parameters({"browserName"})
        public synchronized ThreadLocal<WebDriver> setUP(String browserName) {
            if (browserName.equalsIgnoreCase("chrome")) {
                WebDriverManager.chromedriver().setup();
                driverThreadLocal.set(new ChromeDriver());
            } else if (browserName.equalsIgnoreCase("firefox")) {
                WebDriverManager.firefoxdriver().setup();
                driverThreadLocal.set(new FirefoxDriver());
            } else if (browserName.equalsIgnoreCase("edge")) {
                WebDriverManager.edgedriver().setup();
                driverThreadLocal.set(new EdgeDriver());
            } else if (browserName.equalsIgnoreCase("IE")) {
                WebDriverManager.iedriver().setup();
                driverThreadLocal.set(new InternetExplorerDriver());
            } else {
                System.out.println("Wrong Driver");
            }
driverWait = new webdriverwait(getDriver(),15);
    
            return   driverThreadLocal;
        }
    
        public synchronized  WebDriver getDriver(){
            return driverThreadLocal.get();
        }
    
        public synchronized void browserclose(){
            getDriver().close();
        }
    }

TEST CLASS:有两种情况的测试类

package Parallel_testing;


import com.cross.base.MultiThread_Class;
import org.openqa.selenium.By;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.webdriverwait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;

public class CrossbrowserYouTube_test extends MultiThread_Class {
    @BeforeMethod
    @Parameters({"browserName"})
    public void browserLaunch(String browserName){
        setUP(browserName);
    }


   //@AfterMethod
    public void teardown(){
        browserclose();
    }

    @Test
    public void test1(){
        getDriver().get("https://www.youtube.com/");
        System.out.println("Test 1 Thread id is : " + Thread.currentThread().getId());
        driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).click();
        driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).sendKeys("Automation  ");
        driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//yt-icon[@class='style-scope ytd-searchBox']"))).click();

    }

    @Test
    public void test2(){
        getDriver().get("https://www.youtube.com/");
        System.out.println("Test 2 Thread id is : " + Thread.currentThread().getId());
        driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).click();
        driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='search']"))).sendKeys("  Testing");
        driverWait.until(ExpectedConditions.elementToBeClickable(By.xpath("//yt-icon[@class='style-scope ytd-searchBox']"))).click();

    }


}

XML 文件:- XML 测试文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYstem "http://testng.org/testng-1.0.dtd">
<suite name="All Test Suite" parallel="methods" thread-count="12">

    <parameter name="browserName" value="chrome"/>
    <test verbose="2" preserve-order="true"  name="chrome">
        <classes>
            <class name="Parallel_testing.CrossbrowserYouTube_test">
                <methods>
                    <include name="test1"/>
                    <include name="test2"/>
                </methods>
            </class>
        </classes>
    </test>

</suite>

enter image description here

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。