在Selenium WebDriver测试中使用Page对象时遇到定义Xpath的问题

问题描述 投票:2回答:1

Selenium newbie here ...我正在尝试创建我的第一个测试框架。

测试网站:https://www.phptravels.net/

测试用例 :

  1. 打开浏览器并进入网页
  2. 加载页面后,单击MyAccount - > Login

我在我的页面对象类中使用了xpath,脚本只会在启动网页之前运行。它无法单击“登录”链接。

我试图包含一个隐式等待,假设页面加载所用的时间比平时长。即使这样,问题依然存在。

你能帮我理解一下这个有用的正确xpath吗?

代码:

pom_homepage.Java

package PageObjects;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class POM_HomePage {

    WebDriver driver;

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



    @FindBy(xpath="//*[@id='li_myaccount']/ul/li[1]/a")
    WebElement LinkMyAccount;
    public WebElement clickMyAccount() {
        return LinkMyAccount;
    }


}

homepage.Java

package TestGroupID;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.testng.annotations.Test;

import PageObjects.POM_HomePage;
import Resources.MasterScript;


public class HomePage extends MasterScript{

    @Test
    public void SignIn() throws IOException {
        driver=LoadBrowser();
        LoadPropFile();
        driver.get(prop.getProperty("test_website"));
        POM_HomePage pomHome=new POM_HomePage(driver);
        driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
        if (pomHome.clickMyAccount().isDisplayed()) {
            pomHome.clickMyAccount().click();
            driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
        }
    }
}
java selenium selenium-webdriver pageobjects
1个回答
0
投票

根据您提出的问题,一旦页面加载,请单击MyAccount - > Login。所以你应该在两个单独的WebElements上调用click()方法。但是你的POM_HomePage.java只返回一个WebElement作为@FindBy(xpath="//*[@id='li_myaccount']/ul/li[1]/a")

  • POM_HomePage.java中定义了两个WebElements和两个相关的functions()如下: 我的帐户链接 @FindBy(xpath="//div[@class='navbar']//li[@id='li_myaccount']/a") WebElement LinkMyAccount; public WebElement clickMyAccount() { return LinkMyAccount; } 登录链接 @FindBy(xpath="//div[@class='navbar']//li[@id='li_myaccount']//ul[@class='dropdown-menu']/li/a[contains(.,'Login')]") WebElement LinkLogin; public WebElement clickLogin() { return LinkLogin; }
  • HomePage.java中,将isDisplayed()click()称为WebElements,如下所示: @Test public void SignIn() throws IOException { driver=LoadBrowser(); LoadPropFile(); driver.get(prop.getProperty("test_website")); POM_HomePage pomHome=new POM_HomePage(driver); driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS); if (pomHome.clickMyAccount().isDisplayed()) { pomHome.clickMyAccount().click(); } if (pomHome.clickLogin().isDisplayed()) { pomHome.clickLogin().click(); } }
© www.soinside.com 2019 - 2024. All rights reserved.