Объектная модель страницы и Фабрика страниц в случае списка веб-элементов

#java #selenium-webdriver #foreach #pageobjects #page-factory

Вопрос:

У меня есть n элементов (полей ввода) на веб-странице с общим xpath. Я пытаюсь понять, как заполнить все это поле, используя объектную модель страницы, фабрику страниц и для каждого метода (значение одинаково для всех полей). Все работает правильно, когда я использую каждый метод в тестовом классе.

Проблема в том, что я пытаюсь поместить @findBy в свой класс PageObject. Как найти такие элементы в случае объектной модели страницы с помощью Pagefactory и использовать для каждого метода?

Мой класс POM и тестовый класс, веб-элементы которых находятся в тестовом классе (работают правильно) и расположены в классе POM (сбой) Я не могу использовать веб-элемент @findBy List и ключи отправки в открытом доступе, это точно, но как решить этот случай и поставить для каждого метода?

 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>
 

Ответ №1:

Кажется, я нашел решение. Нет необходимости помещать аннотацию @findBy в класс pom, я поместил для каждого метода в раздел public void с путем поиска. В тестовом классе нам нужно только вызвать значение из public void в pom. Смотрите код ниже.

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