有什么方法可以使用PageFactory框架并行执行测试?

时间:2019-01-17 07:56:53

标签: java selenium-webdriver parallel-processing thread-local page-factory

我想并行执行Test。我有PageFactory框架。当我并行执行脚本时,事情变得一团糟。驱动程序实例被覆盖。

LocalDriveFactory.Java

package com.pom.framework;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;
import com.pom.listeners.TestListener;
import io.github.bonigarcia.wdm.WebDriverManager;

public class LocalDriverFactory {

protected String WORKINGDIR = System.getProperty("user.dir");
static {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy-hh-mm-ss");
    String logPath = System.getProperty("user.dir")+"/Log";
    System.setProperty("current.date.time", dateFormat.format(new Date()));
    System.setProperty("logPath", logPath);
}

static WebDriver createInstance(String browser) {
    WebDriver wbDriver= null;
    EventFiringWebDriver driver;
    TestListener eventListener;
    JavascriptExecutor js;



    if(browser.equals("FF")) {
        WebDriverManager.firefoxdriver().arch32().setup();
        wbDriver=new FirefoxDriver();
    }else if(browser.equals("Chrome")) {
        WebDriverManager.chromedriver().setup();;
        wbDriver=new ChromeDriver();
    }

    driver = new EventFiringWebDriver(wbDriver);
    eventListener = new TestListener();
    driver.register(eventListener);

    js = (JavascriptExecutor)wbDriver;
    return driver;
}

public void navigate(String url) {
    LocalDriverManager.getDriver().navigate().to(url);
}
}

LocalDriverManager.java

package com.pom.framework;
import org.openqa.selenium.WebDriver;
public class LocalDriverManager {


 private static ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();

    public static WebDriver getDriver() {
        return webDriver.get();
    }

    static void setWebDriver(WebDriver driver) {
        webDriver.set(driver);
    }
}

实现了IInvokedMethodListener,以如下方式调用DriverManager的createInstance()方法-

public class WebDriverListener extends LocalDriverFactory implements IInvokedMethodListener  {


    public void beforeInvocation(IInvokedMethod method, ITestResult testResult) {



        if (method.isTestMethod()) {
            String browserName = method.getTestMethod().getXmlTest().getLocalParameters().get("browserName");
            System.out.println(browserName);
            WebDriver driver = LocalDriverFactory.createInstance(browserName);
            LocalDriverManager.setWebDriver(driver);
        }
    }

    public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
        if (method.isTestMethod()) {
            WebDriver driver = LocalDriverManager.getDriver();
            if (driver != null) {
                driver.quit();
            }
        }

    }
}

示例测试脚本-

public class TestScript1{
@Test
@Parameters("URL")
void firstMethod(String url) {

      WebDriver driver = LocalDriverManager.getDriver();
      driver.get(url);
      driver.manage().window().maximize();
      HomePage hp = new HomePage(driver);


}

HomePage.java

public class HomePage {

private WebDriver driver;

public HomePage(WebDriver driver){
     this.driver = driver;
     PageFactory.initElements(driver, this);
}

WebElements & methods


}

当我们调用page方法时,驱动程序将被覆盖。我有测试类,其中包含多个@Test 如何使用PageFactrory并行执行测试?

0 个答案:

没有答案