简体   繁体   中英

Page Object Model and Page Factory in case of list of web elements

I have n-elements (input fields) in webpage with common xpath. I am trying to figure out how to fill up all this field using page object model, page factory and for each method (value is same for all fields). Everything works properly when I am using for each method within test class.

The problem is when I am trying to put @FindBy within my PageObject class. How to locate such elements in the case of page object model with Pagefactory and use for each method?

My POM class and Test class which web elements are within Test class (works properly) and located in POM class (fail) I can't use @FindBy List WebElement and sendKeys in public void that's for sure, but how to solve this case and put for each method as well?

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);
        }
    }
}

Error:

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

Seems that I have found the solution. There is no need to put annotation @FindBy in pom class, I have put for each method under public void with findements path. Within test class we only have to call value from public void in pom. See the code below.

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);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM