0

我正在尝试并行运行两种测试方法。启动测试后,URL 仅在一个浏览器上启动,并在第二次测试时变为空白。我还观察到,如果我删除驱动程序管理()窗口()最大化();删除 Cookies 等,然后为两个测试启动 URL,尽管一个测试被执行,另一个测试失败,并出现错误'stale element reference: element is not attach to the page document'。

谁能告诉我我们如何实现并行测试?

    public class CheckDepartmentsTest extends TestBase{


        public CheckDepartmentsTest() {
            super();

        }
        @BeforeMethod()
        public void setup() throws IOException {
            initialization(URL,pageLoadTimeout,implicitlyWait,ExplicitWait,Path_Current_Direc);
            Loginlogout=new LoginLogout(driver);

        }

        @Test(/*priority=3,*/enabled=true)
        public void Login_LogoutTest() throws IOException {
            Loginlogout.LoginLogoutTest(driver,action,wait);

        }

        @Test(/*priority=2,*/enabled=true)
        public void Search_Product_CheckoutTest() throws InterruptedException, IOException {


            Checkdepartments=Loginlogout.Go_To_Departments(driver,action);
            Checkdepartments.Select_Electronics_Headphones(driver,wait);
            Checkdepartments.Select_MacBook_From_SearchBox(driver,wait);

        }

        @AfterSuite()
        public void teardown() {
            driver.quit();
        }
    }

    Base Class:-

    public class TestBase {

        public  WebDriver driver;
        public  Properties prop;
        public  WebDriverWait wait;
        public  Actions action;
        public String URL;
        public String path;
        public String Path_Current_Direc;
        public String currentDir = "\\QATest\\src\\main\\java\\com\\amazon\\Screenshot";
        public LoginLogout Loginlogout;
        public CheckDepartments Checkdepartments;
        public  long pageLoadTimeout;
        public  long implicitlyWait;
        public  long ExplicitWait;


        public TestBase() {
            try {
                prop= new Properties();
                FileInputStream file=new FileInputStream(TestUtil.Config_File);
                prop.load(file);
                URL=prop.getProperty("URL");
                Path_Current_Direc=System.getProperty("user.dir");
                pageLoadTimeout=15;
                implicitlyWait=10;
                ExplicitWait=8;


            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    public  void initialization(String URL,long pageLoadTimeout,long implicitlyWait ,long ExplicitWait,String path_Current_Direc) throws IOException {

                if(prop.getProperty("Browser").equalsIgnoreCase("firefox")) {
                    System.setProperty("webdriver.gecko.driver",Path_Current_Direc+"\\src\\main\\java\\com\\amazon\\utility\\geckodriver.exe");
                    driver = new FirefoxDriver();
                }
                else {
                    System.setProperty("webdriver.chrome.driver",Path_Current_Direc+"\\src\\main\\java\\com\\amazon\\utility\\chromedriver.exe");
                    driver = new ChromeDriver();
                }
                action=new Actions(driver);
                wait=new WebDriverWait(driver, ExplicitWait);
                driver.get(URL);
                driver.manage().window().maximize();
                driver.manage().timeouts().pageLoadTimeout(pageLoadTimeout, TimeUnit.SECONDS);
                driver.manage().timeouts().implicitlyWait(implicitlyWait, TimeUnit.SECONDS);
    System.out.println("Thread Check  "+ Thread.currentThread().getName());
        }
    }

    TESTNG XML:-
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
    <suite name="Suite" parallel="methods">
      <test thread-count="5" name="Test" parallel="methods">
        <classes>
          <class name="com.amazon.testcases.CheckDepartmentsTest"/>
        </classes>
      </test> <!-- Test -->
    </suite> <!-- Suite -->

    Expected- Both the Test Should Run in Parallel

    Actual- Only one test is getting executed as second one is getting failed
4

1 回答 1

0

您需要使用 ThreadLocal 使 WebDriver 线程安全。当您并行运行该方法时,它会为 TestBase 中定义的 Webdriver 创建问题。请检查下面的代码。您应该将线程本地实现移动到单独的类中。现在以修改您的代码为例。

public class CheckDepartmentsTest extends TestBase{


    public CheckDepartmentsTest() {
        super();

    }
    @BeforeMethod()
    public void setup() throws IOException {
        initialization(URL,pageLoadTimeout,implicitlyWait,ExplicitWait,Path_Current_Direc);
        Loginlogout=new LoginLogout(TestBase.driveSafe.get());

    }

    @Test(/*priority=3,*/enabled=true)
    public void Login_LogoutTest() throws IOException {
        Loginlogout.LoginLogoutTest(TestBase.driveSafe.get(),action,wait);

    }

    @Test(/*priority=2,*/enabled=true)
    public void Search_Product_CheckoutTest() throws InterruptedException, IOException {


        Checkdepartments=Loginlogout.Go_To_Departments(TestBase.driveSafe.get(),action);
        Checkdepartments.Select_Electronics_Headphones(TestBase.driveSafe.get(),wait);
        Checkdepartments.Select_MacBook_From_SearchBox(TestBase.driveSafe.get(),wait);

    }

    @AfterSuite()
    public void teardown() {
        TestBase.driveSafe.get().quit();
    }
}

Base Class:-

public class TestBase {

   // public  WebDriver driver;
    protected static InheritableThreadLocal<WebDriver> driveSafe = new InheritableThreadLocal<WebDriver>();
    public  Properties prop;
  //  public  WebDriverWait wait;
    public  Actions action;
    public String URL;
    public String path;
    public String Path_Current_Direc;
    public String currentDir = "\\QATest\\src\\main\\java\\com\\amazon\\Screenshot";
    public LoginLogout Loginlogout;
    public CheckDepartments Checkdepartments;
    public  long pageLoadTimeout;
    public  long implicitlyWait;
    public  long ExplicitWait;


    public TestBase() {
        try {
            prop= new Properties();
            FileInputStream file=new FileInputStream(TestUtil.Config_File);
            prop.load(file);
            URL=prop.getProperty("URL");
            Path_Current_Direc=System.getProperty("user.dir");
            pageLoadTimeout=15;
            implicitlyWait=10;
            ExplicitWait=8;


        } catch (Exception e) {
            e.printStackTrace();
        }

    }

public  void initialization(String URL,long pageLoadTimeout,long implicitlyWait ,long ExplicitWait,String path_Current_Direc) throws IOException {

            if(prop.getProperty("Browser").equalsIgnoreCase("firefox")) {
                System.setProperty("webdriver.gecko.driver",Path_Current_Direc+"\\src\\main\\java\\com\\amazon\\utility\\geckoTestBase.driveSafe.get().exe");
                TestBase.driveSafe.set(new FirefoxDriver());
            }
            else {
                System.setProperty("webdriver.chrome.driver",Path_Current_Direc+"\\src\\main\\java\\com\\amazon\\utility\\chromeTestBase.driveSafe.get().exe");
                TestBase.driveSafe.set(ChromeDriver());
            }
            action=new Actions(driver);
          //  wait=new WebDriverWait(driver, ExplicitWait);
            TestBase.driveSafe.get().get(URL);
            TestBase.driveSafe.get().manage().window().maximize();
            TestBase.driveSafe.get().manage().timeouts().pageLoadTimeout(pageLoadTimeout, TimeUnit.SECONDS);
            TestBase.driveSafe.get().manage().timeouts().implicitlyWait(implicitlyWait, TimeUnit.SECONDS);
System.out.println("Thread Check  "+ Thread.currentThread().getName());
    }
}

TESTNG XML:-
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Suite" parallel="methods">
  <test thread-count="5" name="Test" parallel="methods">
    <classes>
      <class name="com.amazon.testcases.CheckDepartmentsTest"/>
    </classes>
  </test> <!-- Test -->
</suite> <!-- Suite -->

Expected- Both the Test Should Run in Parallel

Actual- Only one test is getting executed as second one is getting failed
于 2019-07-31T12:07:02.223 回答