【问题标题】:Page Object Model and Page Factory in case of list of web elements网页对象模型和网页工厂在网页元素列表的情况下
【发布时间】:2021-07-28 09:52:56
【问题描述】:

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

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

我的 POM 类和测试类,哪些 Web 元素在测试类中(正常工作)并位于 POM 类中(失败)我不能在公共 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, 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/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>

【问题讨论】:

    标签: java selenium-webdriver foreach pageobjects page-factory


    【解决方案1】:

    看来我已经找到了解决办法。 无需在 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);
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-01
      • 1970-01-01
      • 2011-01-28
      • 1970-01-01
      • 1970-01-01
      • 2020-01-11
      • 1970-01-01
      • 2010-12-28
      相关资源
      最近更新 更多