使用testng并行执行驱动程序时,驱动程序未正确初始化吗?

时间:2019-07-25 13:14:43

标签: selenium-webdriver appium-android

我正在尝试自动并行执行Web和android应用程序。我在网络课程中获取android实例的地方。不知道我在做什么错? 如果我单独执行它们,它将按预期运行,但是通过testng.xml文件执行时会出现异常

TestBase.java

public class TestBase{

    public static WebDriver driver;
    public static Properties prop;
    public static EventFiringWebDriver e_driver;
    public static WebEventListener eventListener;


    public TestBase() {
        try {
            prop = new Properties();
            FileInputStream ip = new FileInputStream(
                    System.getProperty("user.dir") + "/src/main/java/com/sample/config" + "/config.properties");
            prop.load(ip);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }


    public static void initialization() {


        String urlValue = Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("env.baseurl");
        System.out.println("Given URL :" + urlValue);
        String driverValue = Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("driver.name");
        String driverPathValue = Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("webdriver.chrome.driver");

        if(driverValue.equals("chromeDriver")){

            System.setProperty("webdriver.chrome.driver", driverPathValue);
            driver = new ChromeDriver();

        //  e_driver = new EventFiringWebDriver(driver);
            // Now create object of EventListerHandler to register it with
            // EventFiringWebDriver
        //  eventListener = new WebEventListener();
        //  e_driver.register(eventListener);
        //  driver = e_driver;

            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);

            if (prop.getProperty("url") != null) {
                driver.get(prop.getProperty("url"));
            } else {
                driver.get(urlValue);
            }
        }
        else if(driverValue.equals("firefoxDriver")){

            System.setProperty("webdriver.gecko.driver", driverPathValue);
            driver = new FirefoxDriver();

            e_driver = new EventFiringWebDriver(driver);
            // Now create object of EventListerHandler to register it with
            // EventFiringWebDriver
            eventListener = new WebEventListener();
            e_driver.register(eventListener);
            driver = e_driver;

            driver.manage().window().maximize();
            driver.manage().deleteAllCookies();
            driver.manage().timeouts().pageLoadTimeout(TestUtil.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);
            driver.manage().timeouts().implicitlyWait(TestUtil.IMPLICIT_WAIT, TimeUnit.SECONDS);

            if (prop.getProperty("url") != null) {
                driver.get(prop.getProperty("url"));
            } else {
                driver.get(urlValue);
            }
        }
        else if(driverValue.equals("appiumDriver")){

            DesiredCapabilities capabilities = new DesiredCapabilities();
            capabilities.setCapability("platformName", Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("platformName"));
            capabilities.setCapability("deviceName", Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("deviceName"));
            capabilities.setCapability("appPackage", Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("appPackage"));
            capabilities.setCapability("appActivity", Reporter.getCurrentTestResult().getTestContext().getCurrentXmlTest().getParameter("appActivity"));


            try {
                driver = new AndroidDriver<WebElement>(new URL("http://localhost:4723/wd/hub"), capabilities);


            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
            System.out.println("appiumDriver  driver ::"+driver.getClass().getName());
            driver.manage().timeouts().implicitlyWait(80, TimeUnit.SECONDS);

        }

LoginPage.java(用于Web,即chrome)

public class LoginPage extends TestBase{

    //Page Factory - OR:
    @FindBy(name="username")
    WebElement username;

    @FindBy(name="password")
    WebElement password;

    @FindBy(xpath="//input[@type='submit']")
    WebElement loginBtn;

    @FindBy(xpath="//button[contains(text(),'Sign Up')]")
    WebElement signUpBtn;

    @FindBy(xpath="//img[contains(@class,'img-responsive')]")
    WebElement crmLogo;

    //Initializing the Page Objects:
    public LoginPage(){
        PageFactory.initElements(driver, this);
    }


    public String validateLoginPageTitle(){
        System.out.println("Web  ::"+driver.getClass());
        return driver.getTitle();
    }

    public boolean validateCRMImage(){
        System.out.println("Web  ::"+driver.getClass());
        return crmLogo.isDisplayed();
    }

AndroidPage.java(适用于android)

public class AndroidPage extends TestBase{

    @FindBy(id = "icon_cross")
    WebElement iconCross;


    public WebElement getIconCross() {
        return iconCross;
    }

    // Initializing the Page Objects:
    public AndroidPage() {
        PageFactory.initElements(driver, this);
    }

    public void closeNotificationPopup() throws InterruptedException {

        System.out.println("Android ::"+driver.getClass());

        Thread.sleep(3000);
        TouchAction action1 = new TouchAction((PerformsTouchActions) (driver));
        action1.tap(1026, 129).perform();
        Thread.sleep(30000);

    //  getIconCross().click();
    //  JavascriptExecutor js = (JavascriptExecutor) driver;
//      js.executeScript("arguments[0].click();", getIconCross());

        Thread.sleep(25000);

    }
}

AndroidTest

public class AndroidTest extends TestBase {

    AndroiReachPage loginPage;

    public AndroidTest() {
        super();
    }

    @BeforeMethod
    public void setUp() {
        initialization();
        loginPage = new AndroiReachPage();

    }

    @Test
    public void closeWindow() throws InterruptedException {
        loginPage.closeNotificationPopup();

    }

    /*
     * @AfterMethod public void tearDown(){ driver.quit(); }
     */

}

LoginPageTest.java

public class LoginPageTest extends TestBase{
    LoginPage loginPage;
    HomePage homePage;

    public LoginPageTest(){
        super();
    }

    @BeforeMethod
    public void setUp(){
        initialization();
        loginPage = new LoginPage();    
    }

    @Test
    public void loginPageTitleTest(){
        loginPage = new LoginPage();    
        String title = loginPage.validateLoginPageTitle();
        System.out.println("Title ::"+title);
        Assert.assertEquals(title, "Free CRM software for any Business");
    }

    @Test
    public void crmLogoImageTest(){
        loginPage = new LoginPage();    
        boolean flag = loginPage.validateCRMImage();
        Assert.assertTrue(flag);
    }

        /*@Test(priority=3)
    public void loginTest(){
        LoginPage loginPage = new LoginPage();  
        homePage = loginPage.login(prop.getProperty("username"), prop.getProperty("password"));
    }*/

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

/*  @AfterMethod
    public void tearDown(){
        driver.quit();
    }*/





}

TestNG.xml

enter image description here

控制台:

enter image description here

0 个答案:

没有答案