Java 是否返回页面引用作为第页上操作的结果?

Java 是否返回页面引用作为第页上操作的结果?,java,selenium,selenium-webdriver,webdriver,automated-tests,Java,Selenium,Selenium Webdriver,Webdriver,Automated Tests,我正在Selenium测试项目中使用LoadableComponent。我有一个应用程序,它有一个主页(建模为MainPage.java),我可以从中进入登录页面(LoginPage)。我输入了一些凭据,登录表单会再次将我重定向(如果凭据有效)到主页,启用一些额外的功能。因为我使用MainPage作为许多测试的起点,所以我必须使用有效的URL为MainPage实现load()方法。因此,如果我在做以下事情: MainPage mainpage=new MainPage(); mainpage.g

我正在Selenium测试项目中使用
LoadableComponent
。我有一个应用程序,它有一个主页(建模为
MainPage.java
),我可以从中进入登录页面(
LoginPage
)。我输入了一些凭据,登录表单会再次将我重定向(如果凭据有效)到
主页
,启用一些额外的功能。因为我使用
MainPage
作为许多测试的起点,所以我必须使用有效的URL为
MainPage
实现
load()
方法。因此,如果我在做以下事情:

MainPage mainpage=new MainPage();
mainpage.get();
LoginPage loginPage=mainpage.clickLogin();
loginPage.setUser("whatever").setPassword("totally_secret");
mainpage = loginPage.clickJoin();
mainpage.get();
我无法确定将我指向
主页的操作是否成功,因为无论如何,最后一次调用将加载该页面。另一方面,如果调用
clickJoin()
方法而不将返回的对象分配给变量,则无法验证是否加载了正确的页面,也无法获取对其方法的引用以继续测试。我还可以使
clickJoin()
方法返回void,但是如何在测试中获得对新页面的引用呢?
我该怎么处理呢

为什么还要在
登录页面之后调用
mainpage.get()
。单击join()
?与页面上的元素的交互不会带您进入下一个/上一个页面吗

通常,您应该能够立即与从
clickJoin()
返回的
mainPage
对象进行交互您不必再次调用
get()
。如果在没有它的情况下出现错误,可能是因为代码执行得太快,没有真正等待页面正确加载

为了避免这种情况,您可以使用
loginPage.clickJoin()
方法确保重新加载主页面

public MainPage clickJoin() {

    joinLink.click(); //clicking some link/button or whatever should take you to the page

    new WebDriverWait(driver, timeToWait).until(
       //here you can use the ExpectedConditions class to wait for the title 
       //to change to a specified value or something similar. There's a wide
       //variety of methods provided. Alternatively, you can implement the
       // ExpectedCondition interface yourself
    );
    return new MainPage(driver);
}
如果页面加载失败,您将得到一个异常,测试将失败


您不必使用与上面的伪代码完全相同的方法,但是
WebDriverWait
似乎是一种确保在它们之间导航时加载正确页面的方法。

为什么还要在
loginPage.clickJoin()之后调用
mainpage.get()
?与页面上的元素的交互不会带您进入下一个/上一个页面吗

通常,您应该能够立即与从
clickJoin()
返回的
mainPage
对象进行交互您不必再次调用
get()
。如果在没有它的情况下出现错误,可能是因为代码执行得太快,没有真正等待页面正确加载

为了避免这种情况,您可以使用
loginPage.clickJoin()
方法确保重新加载主页面

public MainPage clickJoin() {

    joinLink.click(); //clicking some link/button or whatever should take you to the page

    new WebDriverWait(driver, timeToWait).until(
       //here you can use the ExpectedConditions class to wait for the title 
       //to change to a specified value or something similar. There's a wide
       //variety of methods provided. Alternatively, you can implement the
       // ExpectedCondition interface yourself
    );
    return new MainPage(driver);
}
如果页面加载失败,您将得到一个异常,测试将失败


您不必使用与上面的伪代码完全相同的方法,但是
WebDriverWait
似乎是一种确保在它们之间导航时加载正确页面的方法。

为什么还要在
loginPage.clickJoin()之后调用
mainpage.get()
?与页面上的元素的交互不会带您进入下一个/上一个页面吗

通常,您应该能够立即与从
clickJoin()
返回的
mainPage
对象进行交互您不必再次调用
get()
。如果在没有它的情况下出现错误,可能是因为代码执行得太快,没有真正等待页面正确加载

为了避免这种情况,您可以使用
loginPage.clickJoin()
方法确保重新加载主页面

public MainPage clickJoin() {

    joinLink.click(); //clicking some link/button or whatever should take you to the page

    new WebDriverWait(driver, timeToWait).until(
       //here you can use the ExpectedConditions class to wait for the title 
       //to change to a specified value or something similar. There's a wide
       //variety of methods provided. Alternatively, you can implement the
       // ExpectedCondition interface yourself
    );
    return new MainPage(driver);
}
如果页面加载失败,您将得到一个异常,测试将失败


您不必使用与上面的伪代码完全相同的方法,但是
WebDriverWait
似乎是一种确保在它们之间导航时加载正确页面的方法。

为什么还要在
loginPage.clickJoin()之后调用
mainpage.get()
?与页面上的元素的交互不会带您进入下一个/上一个页面吗

通常,您应该能够立即与从
clickJoin()
返回的
mainPage
对象进行交互您不必再次调用
get()
。如果在没有它的情况下出现错误,可能是因为代码执行得太快,没有真正等待页面正确加载

为了避免这种情况,您可以使用
loginPage.clickJoin()
方法确保重新加载主页面

public MainPage clickJoin() {

    joinLink.click(); //clicking some link/button or whatever should take you to the page

    new WebDriverWait(driver, timeToWait).until(
       //here you can use the ExpectedConditions class to wait for the title 
       //to change to a specified value or something similar. There's a wide
       //variety of methods provided. Alternatively, you can implement the
       // ExpectedCondition interface yourself
    );
    return new MainPage(driver);
}
如果页面加载失败,您将得到一个异常,测试将失败


您不必使用与上面的伪代码完全相同的方法,但是
WebDriverWait
似乎是一种确保在它们之间导航时加载正确页面的方法。

不确定我是否100%清楚您的要求,但我想我会这样做。页面对象上的公共方法应该更能代表用户操作(成功登录),而不是单个操作(填写单个字段)

我已经在主页构造函数中添加了loggedIn标志,这可能有助于确定哪些操作可用/应该可用

class MainPage {   
    public MainPage(bool loggedIn) {
      ...
    } 
}

class LoginPage {   
    public LoginPage(){
       ...
    }

    public MainPage loginExpectSuccess(string username, string password) {
        setUser(username);
        setPassword(password);
        clickJoin();

        return new MainPage(true);
    }

    public LoginPage loginExpectFailure(string username, string password) {
        setUser(username);
        setPassword(password);
        clickJoin();

        return this;
    } 

    private void setUser(string username) {
        ...
    }

    private void setPassword(string password) {
        ...
    }

    private void clickJoin() {
        ...
    }
}
这样,您的测试可以简化为:

MainPage mainpage = new MainPage(false);
LoginPage loginPage=mainpage.clickLogin();
mainpage = loginPage.loginExpectSuccess("whatever", "totally_secret");
...
mainpage.nextUserAction(); //Do whatever a logged in user should be able to do, if user is not actually logged in successfully this will fail

我不确定我是否100%清楚你在问什么,但以下是我想我会做的。页面对象上的公共方法应该更能代表用户操作(成功登录),而不是单个操作(填写单个字段)

我已经在主页构造函数中添加了loggedIn标志,这可能有助于确定哪些操作可用/应该可用

class MainPage {   
    public MainPage(bool loggedIn) {
      ...
    } 
}

class LoginPage {   
    public LoginPage(){
       ...
    }

    public MainPage loginExpectSuccess(string username, string password) {
        setUser(username);
        setPassword(password);
        clickJoin();

        return new MainPage(true);
    }

    public LoginPage loginExpectFailure(string username, string password) {
        setUser(username);
        setPassword(password);
        clickJoin();

        return this;
    } 

    private void setUser(string username) {
        ...
    }

    private void setPassword(string password) {
        ...
    }

    private void clickJoin() {
        ...
    }
}
这样,您的测试可以简化为:

MainPage mainpage = new MainPage(false);
LoginPage loginPage=mainpage.clickLogin();
mainpage = loginPage.loginExpectSuccess("whatever", "totally_secret");
...
mainpage.nextUserAction(); //Do whatever a logged in user should be able to do, if user is not actually logged in successfully this will fail

我不确定我是否100%清楚你在问什么,但以下是我想我会做的。页面对象上的公共方法应该更具代表性