java - testNG 并行执行不起作用

标签 java selenium selenium-webdriver testng

我尝试使用 testNG 对两个浏览器并行运行以下测试,同时运行两个浏览器都使用 URL 启动,但仅在一个浏览器上执行完整的测试。

这是我的测试套件类(class)

@Test (groups = {"Enable"}) 
@SuppressWarnings("unused")
public class EETestSuite_01 extends ApplicationFunctions{
    String URL = Globals.GC_EMPTY;

    @BeforeTest
    @Parameters("browser")
    public void loadTest(String browser) throws IOException{
        InitializeTestEnv("EE|BizApp");
        if(browser.equalsIgnoreCase("Firefox"))
               GetBrowser("Firefox");
            else if(browser.equalsIgnoreCase("Chrome")){
               GetBrowser("Chrome");
            }

    }

    @AfterMethod
    public void cleartest() throws InterruptedException{
        driver.close();
        driver.quit();
        driver = null;
    }


    public void TC001_Phone_First_Acquisition_Journey_PAYM() throws InterruptedException{
        URL = EnvDetail.get(Globals.GC_HOME_PAGE);
        Map<String,String> TDChoosePlan = null;
        TDChoosePlan = getData(appName+Globals.GC_TEST_DATA_SHEET,"ChoosePlan",1);
        try{
            launchApp(URL); 
            //driver.navigate().to("javascript:document.getElementById('overridelink').click()");
            EEHomePage homePage = PageFactory.initElements(driver, EEHomePage.class);
            EEShopPage shopPage = homePage.GetToShopPage();
            EEPhoneMatrixPage phonePage = shopPage.GetToPhoneMatrixPage();
            EEChoosePlanPage planPage = phonePage.ChoosePhone("NokiaLumia1020"); // Implement select phone
            EEAddonsPage addonPage = planPage.SelectPhonesPlan(TDChoosePlan);
            EEBasket basketPage = addonPage.GoToBasketPage();
            EESecureCheckOut secureChkOutPage = basketPage.GoToSecureCheckOutPage();
            secureChkOutPage.ChooseNonExistingCustomer();
            EEConfirmation confPage = secureChkOutPage.FillUserRegisterForm(2);
        }catch(Exception e){
            e.printStackTrace();
        }       
    }


}

我的 XML 看起来像这样

<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name = "EEAutomationTestSuite" verbose="2" parallel = "tests" thread-count="100">

<test name="PAYM Acquisition in Chrome">
  <parameter name="browser" value="Firefox"></parameter>
   <classes>
     <class name="com.testsuite.EETestSuite_01">
    </class>
  </classes>
</test>

<test name="PAYM Acquisition in FF">
  <parameter name="browser" value="Firefox"></parameter>
   <classes>
     <class name="com.testsuite.EETestSuite_01">
    </class>
  </classes>
</test>
</suite>

我的主页代码是这样的

*/  public EEShopPage GetToShopPage() throws InterruptedException{

       // longWait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector(OR.getProperty("wblShopHeader"))));   
        lblShopHeader = driver.findElement(By.cssSelector(OR.getProperty("wblShopHeader")));
        Actions builder = new Actions(driver); 
        Actions hoverOverRegistrar = builder.moveToElement(lblShopHeader);
        hoverOverRegistrar.perform();Thread.sleep(10000);
        lnkStartShopping = driver.findElement(By.cssSelector(OR.getProperty("lnkStartShopping")));
        mediumWait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(OR.getProperty("lnkStartShopping"))));
        lnkStartShopping.click();
        return PageFactory.initElements(driver,EEShopPage.class );
    }
}

这是驱动程序

public static void GetBrowser(String browser){
        try{
            if (browser.equalsIgnoreCase("firefox")) {              
//              FirefoxProfile firefoxProfile = new FirefoxProfile();
//              File pathToBinary = new File(Globals.GC_FIREFOX_BIN_PATH);
//              FirefoxBinary ffBinary = new FirefoxBinary(pathToBinary);
                //firefoxProfile.setPreference("webdriver.load.strategy","unstable");
                driver = new FirefoxDriver();
            } else if (browser.equalsIgnoreCase("iexplorer")){
                System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + 
                                   "//resource//drivers//IEDriverServer.exe");          
                DesiredCapabilities capabilities = new DesiredCapabilities(); 
                capabilities.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
                driver = new InternetExplorerDriver(capabilities);
            } else if (browser.equalsIgnoreCase("chrome")){
                System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + 
                                   "//resource//drivers//chromedriver.exe");

我只是猜测主页中有一个悬停操作,对于一个浏览器来说它工作正常,但对于另一种浏览器则没有任何反应......是否是由于焦点问题?

请让我知道如何通过示例解决此问题

最佳答案

我无法从您的代码中看出您正在为页面对象 EEHomePage 使用哪个构造函数。

因为,如果您使用默认构造函数,那么您的 PageFactory 将无法初始化您的 Web 元素,除非它们由 @FindBy 注释定义,

PageFactory 将 webDriver 和 Object 类作为参数,并使用提供的 webDriver 在内部初始化该 Object 类。这可以通过以下两种方式实现:

1) 使用 @FindBy 注释在 pageObjects 中定义 webElements,如下所示:

 @FindBy(css=//your locator value here)
    private WebElement lblShopHeader;

或者

2) 定义构造函数并通过 PageFactory 提供的 webdriver 初始化页面对象 webdriver,如下所示:

EEHomeShop(WebDriver driver){

   this.driver=driver;

  }

关于java - testNG 并行执行不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20210152/

相关文章:

java - Selenium 3.0 不适用于 Firefox 51.0.1

java - Selenium 网络驱动程序(Eclipse): importing issues using ctrl +shift +O

java - 使用不同的定位器定位 WebElement(NoSuchElementException)

C# Selenium Webdriver 在 iframe 中查找元素

java - Java 的 SSH 库,它很好地支持多线程!

java - 使用模板模式设计通用流程

java - 如何在 Java 9 中获取 Java 应用程序加载的所有 jar?

python - 无法从网页获取动态生成的内容

java - 如何使用 WebDriver 查找按钮?

java - 可完成的 future 。处理业务 "exceptions"的最佳方式是什么?