Selenium Webdriver - Java,PageObject,PageFactory -

时间:2015-11-09 02:49:51

标签: java selenium-webdriver

我正在使用Selenium Webdriver - 页面对象模型与Page Factory进行自动化。

问题:点击提交控件后停留在同一页面上并且不加载下一页,因为我在下一页检查的元素显示为代理元素。

问题: 我的代码中有什么不对吗? 我没有等待元素出现的时间吗? 我在UberDashboard上使用了错误的定位器吗? - 我最初使用div classname,但firebug无法使用它找到元素,因此切换到span(span.user-name-display),它与firebug一起工作正常。

我在stackoverflow上发现了很多问题,并参考了许多关于页面工厂的教程和博客,我发现我的代码没有任何不正确之处,但是一双新的眼睛可能指向我错过的东西。

非常感谢有关如何解决此问题的任何指示。

LoginPage.java

package pages;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.Test;

public class LoginPage extends BasePage {

    private WebDriver driver;
    private WebDriverWait wait;

    public LoginPage(WebDriver driver) {
        this.driver = driver;
    }

    //ELEMENTS
    @FindBy(id = "user_session_user_name")
    @CacheLookup
    private WebElement usernameField;

    @FindBy(id = "user_session_password")
    @CacheLookup
    private WebElement passwordField;

    @FindBy(id = "user_session_submit")
    @CacheLookup
    private WebElement signInBtn;

    //METHODS

    public void provideUsername(String email) {
        System.out.println("Waiting for visibility of the element: " + usernameField + "...");
        explicitWait(driver, usernameField);
        System.out.println("Providing the username...");
        usernameField.sendKeys(email);
    }

    public void providePassword(String password) {
        System.out.println("Waiting for visibility of the element: " + passwordField + "...");
        explicitWait(driver, passwordField);
        System.out.println("Providing the password...");
        passwordField.sendKeys(password);
    }

    public UberDashboard clickSignInBtnExistingUser() {
        System.out.println("Clicking on Sign In button...");
        signInBtn.click();
        return PageFactory.initElements(driver, UberDashboard.class);
    }
}

UberDashboard.java

package pages;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.CacheLookup;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

public class UberDashboard extends BasePage {

    private WebDriver driver;

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

    // ELEMENTS

    @FindBy(css = "span.user-name-display")
    @CacheLookup
    private WebElement username;

    // METHODS
    public String getUsername() {
        System.out.println("Waiting for visibility of the element: " + username
                + "...");
        explicitWait(driver, username);
        System.out.println("Getting the username...");
        return username.getText();
    }
}

LoginTest.java

package tests;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver.Navigation;
import org.openqa.selenium.WebDriver.Options;
import org.openqa.selenium.WebDriver.TargetLocator;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.PageFactory;

import pages.LoginPage;
import pages.UberDashboard;

public class LoginTest {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get(URL); //points to login page
        String email = "abc@xyz.com";
        String password = "password";

        LoginPage loginPage = PageFactory.initElements(driver, LoginPage.class);

        loginPage.getWelcomeBackMsg();
        loginPage.provideUsername(email);
        loginPage.providePassword(password);

        UberDashboard uberDashboard = loginPage.clickSignInBtnExistingUser();

        uberDashboard.getUsername();

        driver.close();
        driver.quit();

    }

}

输出:

Waiting for visibility of the element: [[FirefoxDriver: firefox on MAC (f5b40b1b-9a77-5746-b294-82472cfbfda8)] -> css selector: #main-content-container div div h2]...
Getting the Welcome message...
Waiting for visibility of the element: [[FirefoxDriver: firefox on MAC (f5b40b1b-9a77-5746-b294-82472cfbfda8)] -> id: user_session_user_name]...
Providing the username...
Waiting for visibility of the element: [[FirefoxDriver: firefox on MAC (f5b40b1b-9a77-5746-b294-82472cfbfda8)] -> id: user_session_password]...
Providing the password...
Clicking on Sign In button...
Waiting for visibility of the element: Proxy element for: org.openqa.selenium.support.pagefactory.DefaultElementLocator@7205765b...
Exception in thread "main" org.openqa.selenium.TimeoutException: Timed out after 60 seconds waiting for visibility of Proxy element for: org.openqa.selenium.support.pagefactory.DefaultElementLocator@7205765b
Driver info: org.openqa.selenium.firefox.FirefoxDriver
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:80)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:229)
    at pages.BasePage.explicitWait(BasePage.java:25)
    at pages.UberDashboard.getUsername(UberDashboard.java:51)
    at tests.LoginTest.main(LoginTest.java:49)
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span.user-name-display"}
Command duration or timeout: 10.72 seconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities [{applicationCacheEnabled=true, rotatable=false, handlesAlerts=true, databaseEnabled=true, version=42.0, platform=MAC, nativeEvents=false, acceptSslCerts=true, webStorageEnabled=true, locationContextEnabled=true, browserName=firefox, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
*** Element info: {Using=css selector, value=span.user-name-display}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:595)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:348)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByCssSelector(RemoteWebDriver.java:437)
    at org.openqa.selenium.By$ByCssSelector.findElement(By.java:426)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:340)
    at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
    at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
    at com.sun.proxy.$Proxy3.isDisplayed(Unknown Source)
    at org.openqa.selenium.support.ui.ExpectedConditions.elementIfVisible(ExpectedConditions.java:302)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$100(ExpectedConditions.java:41)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:288)
    at org.openqa.selenium.support.ui.ExpectedConditions$10.apply(ExpectedConditions.java:285)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:209)
    ... 3 more
Caused by: org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"css selector","selector":"span.user-name-display"}
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Driver info: driver.version: unknown
    at <anonymous class>.FirefoxDriver.prototype.findElementInternal_(file:///var/folders/wb/r1svxhyj6j50lf0301z84tcr0000gn/T/anonymous6146796425192466959webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:10667)
    at <anonymous class>.fxdriver.Timer.prototype.setTimeout/<.notify(file:///var/folders/wb/r1svxhyj6j50lf0301z84tcr0000gn/T/anonymous6146796425192466959webdriver-profile/extensions/fxdriver@googlecode.com/components/driver-component.js:623)

0 个答案:

没有答案