微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

网页元素列表中的页面对象模型和页面工厂

如何解决网页元素列表中的页面对象模型和页面工厂

我在具有通用 xpath 的网页中有 n 个元素(输入字段)。我试图弄清楚如何使用页面对象模型、页面工厂和每种方法(所有字段的值都相同)来填充所有这些字段。当我在测试类中使用每个方法时,一切正常。

问题是当我试图将 @FindBy 放入我的 PageObject 类时。在Pagefactory的页面对象模型的情况下,如何定位这些元素并用于各个方法

我的 POM 类和 Test 类中的 Web 元素在 Test 类中(正常工作)并位于 POM 类中(失败)我不能在 public void 中使用 @FindBy List WebElement 和 sendKeys,这是肯定的,但是如何解决这种情况并为每种方法加上

package cmm.xbid.pageObjects;
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;

public class capacityOverviewPage50HzTPlc {
    WebDriver ldriver;
    public capacityOverviewPage50HzTPlc(WebDriver rdriver){
        ldriver=rdriver;
        PageFactory.initElements(rdriver,this);
    }
    @FindBy(xpath = "//*//body/form/ol/li[3]/a")
    @CacheLookup
    WebElement clickConnectorMenu;

    @FindBy(xpath = "//*//body/form/div[2]/ol[3]/li[1]/a")
    @CacheLookup
    WebElement setUp50HzTPLCBorder;

    @FindBy(xpath = "//*//button[1]/span")
    @CacheLookup
    WebElement buttonUpdate;

    @FindBy(xpath = "//*//div[3]/button[3]")
    @CacheLookup
    WebElement buttonSubmit;

    public void connectorMenu(){
        clickConnectorMenu.click();
    }
    public void hit50HzTPLCBorder() {
        setUp50HzTPLCBorder.click();
    }
    public void clickBtnUpdate(){
        buttonUpdate.click();
    }
    public void clickBtnSubmit(){
        buttonSubmit.click();
    }
}

package cmm.xbid.testCases;
import cmm.xbid.pageObjects.capacityOverviewPage50HzTPlc;
import cmm.xbid.pageObjects.loginPage;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

public class TC_CmmEditNtc50HzTPLC_005 extends baseClass{

    @Test
    public void editNtcValue50Hz() throws FindFailed,InterruptedException {
        loginPage lp=new loginPage(driver);

        Screen scr = new Screen();
        Pattern ptnUser = new Pattern("C:/Users/Desktop/uname.PNG");
        scr.type(ptnUser,"P");
        logger.info("Entered username");
        Pattern ptnPass = new Pattern("C:/Users/upass.PNG");
        scr.type(ptnPass,"X");
        logger.info("Entered password");
        Pattern ptnSignIn = new Pattern("C:/Users/Desktop/signin.PNG");
        scr.click(ptnSignIn);
        logger.info("Sign In button has been successfully clicked");

        Thread.sleep(2000);

        capacityOverviewPage50HzTPlc edit=new capacityOverviewPage50HzTPlc(driver);

        edit.connectorMenu();
        logger.info("Interconnector button has been successfully clicked");
        edit.hit50HzTPLCBorder();
        logger.info("Providing new NTC Values");
        for (WebElement el: driver.findElements(By.xpath("//*//input[@maxlength='11']"))) {
            el.sendKeys("1000");
        }
        edit.clickBtnUpdate();
        logger.info("Update button has been clicked");
        edit.clickBtnSubmit();
        logger.info("Submit button has been clicked");

        boolean res=driver.getPageSource().contains("NTC adjusted for 50HzT-PLC on");
        if (res==true){
            logger.info("Test case passed...");
            Assert.assertTrue(true);
        }
        else{
            logger.info("Test case Failed...");
            Assert.assertTrue(false);
            }
        }
    }
package cmm.xbid.pageObjects;
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 java.util.List;

public class capacityOverviewPage50HzTPlc {
    WebDriver ldriver;
    public capacityOverviewPage50HzTPlc(WebDriver rdriver){
        ldriver=rdriver;
        PageFactory.initElements(rdriver,this);
    }
    @FindBy(xpath = "//*//body/form/ol/li[3]/a")
    @CacheLookup
    WebElement clickConnectorMenu;

    @FindBy(xpath = "//*//body/form/div[2]/ol[3]/li[1]/a")
    @CacheLookup
    WebElement setUp50HzTPLCBorder;

    @FindBy(xpath = "//*//input[@maxlength='11']")
    List<WebElement> newNtcValue;

    @FindBy(xpath = "//*//button[1]/span")
    @CacheLookup
    WebElement buttonUpdate;

    @FindBy(xpath = "//*//div[3]/button[3]")
    @CacheLookup
    WebElement buttonSubmit;

    public void connectorMenu(){
        clickConnectorMenu.click();
    }
    public void hit50HzTPLCBorder() {
        setUp50HzTPLCBorder.click();
    }
    public void setNewValue(String newValue){
        newNtcValue.sendKeys(newValue);
    }
    public void clickBtnUpdate(){
        buttonUpdate.click();
    }
    public void clickBtnSubmit(){
        buttonSubmit.click();
    }
}

package cmm.xbid.testCases;
import cmm.xbid.pageObjects.capacityOverviewPage50HzTPlc;
import cmm.xbid.pageObjects.loginPage;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

public class TC_CmmEditNtc50HzTPLC_005 extends baseClass{

    @Test
    public void editNtcValue50Hz() throws FindFailed,"P");
        logger.info("Entered username");
        Pattern ptnPass = new Pattern("C:/Users/Desktop/upass.PNG");
        scr.type(ptnPass,"X");
        logger.info("Entered password");
        Pattern ptnSignIn = new Pattern("C:/Users/Desktop/signin.PNG");
        scr.click(ptnSignIn);
        logger.info("Sign In button has been successfully clicked");

        Thread.sleep(2000);

        capacityOverviewPage50HzTPlc edit=new capacityOverviewPage50HzTPlc(driver);

        edit.connectorMenu();
        logger.info("Interconnector button has been successfully clicked");
        edit.hit50HzTPLCBorder();
        logger.info("Providing new NTC Values");
        edit.setNewValue("600");
        edit.clickBtnUpdate();
        logger.info("Update button has been clicked");
        edit.clickBtnSubmit();
        logger.info("Submit button has been clicked");

        boolean res=driver.getPageSource().contains("NTC adjusted for 50HzT-PLC on");
        if (res==true){
            logger.info("Test case passed...");
            Assert.assertTrue(true);
        }
        else{
            logger.info("Test case Failed...");
            Assert.assertTrue(false);
        }
    }
}

错误

java: cannot find symbol
  symbol:   method sendKeys(java.lang.String)
  location: variable newNtcValue of type java.util.List<org.openqa.selenium.WebElement>

解决方法

看来我已经找到了解决方案。 不需要在 pom 类中添加注释 @FindBy,我已经将每个方法放在 public void 下,并带有 findements 路径。在测试类中,我们只需要从 pom 中的 public void 中调用 value。 请参阅下面的代码。

package cmm.xbid.pageObjects;
import org.openqa.selenium.By;
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;

public class capacityOverviewPage50HzTPlc {
    WebDriver ldriver;

    public capacityOverviewPage50HzTPlc(WebDriver rdriver) {
        ldriver = rdriver;
        PageFactory.initElements(rdriver,this);
    }

    @FindBy(xpath = "//*//body/form/ol/li[3]/a")
    @CacheLookup
    WebElement clickConnectorMenu;

    @FindBy(xpath = "//*//body/form/div[2]/ol[3]/li[1]/a")
    @CacheLookup
    WebElement setUp50HzTPLCBorder;

    @FindBy(xpath = "//*//button[1]/span")
    @CacheLookup
    WebElement buttonUpdate;

    @FindBy(xpath = "//*//div[3]/button[3]")
    @CacheLookup
    WebElement buttonSubmit;

    public void connectorMenu() {
        clickConnectorMenu.click();
    }

    public void hit50HzTPLCBorder() {
        setUp50HzTPLCBorder.click();
    }

    public void ntcValue(String newValue) {
        for (WebElement values : ldriver.findElements(By.xpath("//*//input[@maxlength='11']"))) {
            values.sendKeys(newValue);

        }
    }

    public void clickBtnUpdate() {
        buttonUpdate.click();
    }

    public void clickBtnSubmit() {
        buttonSubmit.click();
    }
}
package cmm.xbid.testCases;
import cmm.xbid.pageObjects.capacityOverviewPage50HzTPlc;
import cmm.xbid.pageObjects.loginPage;
import org.junit.Assert;
import org.junit.Test;
import org.sikuli.script.FindFailed;
import org.sikuli.script.Pattern;
import org.sikuli.script.Screen;

public class TC_CmmEditNtc50HzTPLC_005 extends baseClass{

    @Test
    public void editNtcValue50Hz() throws FindFailed,InterruptedException {
        loginPage lp = new loginPage(driver);

        Screen scr = new Screen();
        Pattern ptnUser = new Pattern("C:/Users/Desktop/uname.PNG");
        scr.type(ptnUser,"P");
        logger.info("Entered username");
        Pattern ptnPass = new Pattern("C:/Users/Desktop/upass.PNG");
        scr.type(ptnPass,"X");
        logger.info("Entered password");
        Pattern ptnSignIn = new Pattern("C:/Users/mserafin/Desktop/signin.PNG");
        scr.click(ptnSignIn);
        logger.info("Sign In button has been successfully clicked");

        Thread.sleep(2000);

        capacityOverviewPage50HzTPlc edit = new capacityOverviewPage50HzTPlc(driver);

        edit.connectorMenu();
        logger.info("Interconnector button has been successfully clicked");
        edit.hit50HzTPLCBorder();
        logger.info("Providing new NTC Values");
        edit.ntcValue("1000");
        edit.clickBtnUpdate();
        logger.info("Update button has been clicked");
        edit.clickBtnSubmit();
        logger.info("Submit button has been clicked");

        boolean res = driver.getPageSource().contains("NTC adjusted for 50HzT-PLC on");
        if (res == true) {
            logger.info("Test case passed...");
            Assert.assertTrue(true);
        } else {
            logger.info("Test case failed...");
            Assert.assertTrue(false);
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。