Selenium Page Object Reuse

スコット:

私は慣例的にセレン2がどのようにPageObjectsをPOJOとして使用し、次にPageFactoryを使用してこのクラスのフィールドをインスタンス化するかを本当に気に入っています。

私が制限しているのは、さまざまなページで多くの要素を再利用することです。大きな問題は、これらの再利用されたコンポーネントが異なるページに表示されたときに、同じID /名前を持たないことです。ただし、それぞれに対して実行するテストは同じです。

例として、多くの場所で日付を収集します。したがって、このためのサンプルページオブジェクトは(月、日フィールドを削除)のようになります。

public class DatePageObject {
    private WebDriver driver;

    DatePageObject(WebDriver driver) {
        this.driver = driver;
    }

    @FindBy( id = "someIdForThisInstance")
    private WebElement year;

    public void testYearNumeric() {
        this.year.sendKeys('aa');
        this.year.submit();
        //Logic to determine Error message shows up
    }
}

次に、以下のコードでこれを簡単にテストできます。

public class Test {
    public static void main(String[] args) {
         WebDriver driver = new FirefoxDriver();
         DatePageObject dpo = PageFactory.initElements(driver, DriverPageObject.class);
         driver.get("Some URL");
         dpo.testYearNumeric();
    }
}

私が本当にやりたいのは、Springを使用してid / name / xpathなどをアプリケーションに挿入できるように設定することです。

PageFactoryを利用する機能を失うことなくこれを行う方法はありますか?

編集1-カスタムロケーターとファクトリーで作業する、理想的な基本レベルのクラスを追加します。

public class PageElement {
    private WebElement element;
    private How how;
    private String using;

    PageElement(How how, String using) {
        this.how = how;
        this.using = using;
    }
    //Getters and Setters
}


public class PageWidget {
    private List<PageElement> widgetElements;
}


public class Screen {
    private List<PageWidget> fullPage;
    private WebDriver driver;

    public Screen(WebDriver driver) {
        this.driver = driver;
        for (PageWidget pw : fullPage) {
            CustomPageFactory.initElements(driver, pw.class);
        }
}

編集2-メモと同じように、Selenium 2.0.a5以降を実行している限り、ドライバーに暗黙のタイムアウト値を指定できます。

したがって、コードを次のコードに置き換えることができます。

private class CustomElementLocator implements ElementLocator {
    private WebDriver driver;
    private int timeOutInSeconds;
    private final By by;


    public CustomElementLocator(WebDriver driver, Field field,
            int timeOutInSeconds) {
        this.driver = driver;
        this.timeOutInSeconds = timeOutInSeconds;
        CustomAnnotations annotations = new CustomAnnotations(field);
        this.by = annotations.buildBy();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); //Set this value in a more realistic place
    }


    public WebElement findElement() {
        return driver.findElement(by);
    }
}
セルギ・ポシャロフ:

共通Web要素のページオブジェクトを構築できます(この名前を発明したのは:))。各CWEは、異なるページで使用される「ウィジェット」を表します。あなたの例では、これはある種の日付ウィジェットです-年、月、日が含まれています。基本的にはページオブジェクトになります。

PageFactory文字列定数を@FindBy注釈で使用する必要があります

この制限を解決するために、独自ElementLocatorを作成しました

DateWidgetテストでを使用できます。

....
DateWidget widget = new DateWidget(driver, "yearId", "monthId", "dayId");
....

public void testYearNumeric() {
        widget.setYear("aa");
        widget.submit();
        //Logic to determine Error message shows up

        // ... and day
        widget.setDay("bb");
        widget.submit();
        //Logic to determine Error message shows up
    }

DateWidgetカスタムロケータと注釈パーサが含まれているクラスは、次のとおりです。

package pagefactory.test;

import java.lang.reflect.Field;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.pagefactory.Annotations;
import org.openqa.selenium.support.pagefactory.ElementLocator;
import org.openqa.selenium.support.pagefactory.ElementLocatorFactory;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;

public class DateWidget {

    // These constants are used to identify that they should be changed to the actual IDs
    private static final String YEAR_ID = "$YEAR_ID$";
    private static final String MONTH_ID = "$MONTH_ID$";
    private static final String DAY_ID = "$DAY_ID$";

    // Elements whose ids will be replaced during run-time
    /** Year element */
    @FindBy(id = YEAR_ID)
    private WebElement year;

    /** Month element */
    @FindBy(id = MONTH_ID)
    private WebElement month;

    /** day element */
    @FindBy(id = DAY_ID)
    private WebElement day;

    // The ids of the elements
    /** ID of the year element */
    private String yearId;

    /** ID of the month element */
    private String monthId;

    /** ID of the day element */
    private String dayId;

    public DateWidget(WebDriver driver, String yearId, String monthId,
            String dayId) {
        this.yearId = yearId;
        this.monthId = monthId;
        this.dayId = dayId;

        PageFactory.initElements(new CustomLocatorFactory(driver, 15), this);
    }

    public String getYear() {
        return year.getValue();
    }

    public void setYear(String year) {
        setValue(this.year, year);
    }

    public String getMonth() {
        return month.getValue();
    }

    public void setMonth(String month) {
        setValue(this.month, month);
    }

    public String getDay() {
        return day.getValue();
    }

    public void setDay(String day) {
        setValue(this.day, day);
    }

    public void submit() {
        year.submit();
    }

    private void setValue(WebElement field, String value) {
        field.clear();
        field.sendKeys(value);
    }

    private class CustomLocatorFactory implements ElementLocatorFactory {
        private final int timeOutInSeconds;
        private WebDriver driver;

        public CustomLocatorFactory(WebDriver driver, int timeOutInSeconds) {
            this.driver = driver;
            this.timeOutInSeconds = timeOutInSeconds;
        }

        public ElementLocator createLocator(Field field) {
            return new CustomElementLocator(driver, field, timeOutInSeconds);
        }
    }

    private class CustomElementLocator implements ElementLocator {
        private WebDriver driver;
        private int timeOutInSeconds;
        private final By by;

        public CustomElementLocator(WebDriver driver, Field field,
                int timeOutInSeconds) {
            this.driver = driver;
            this.timeOutInSeconds = timeOutInSeconds;
            CustomAnnotations annotations = new CustomAnnotations(field);
            this.by = annotations.buildBy();
        }

        @Override
        public WebElement findElement() {
            ExpectedCondition<Boolean> e = new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver d) {
                    d.findElement(by);
                    return Boolean.TRUE;
                }
            };
            Wait<WebDriver> w = new WebDriverWait(driver, timeOutInSeconds);
            w.until(e);

            return driver.findElement(by);
        }
    }

    private class CustomAnnotations extends Annotations {

        public CustomAnnotations(Field field) {
            super(field);
        }

        @Override
        protected By buildByFromShortFindBy(FindBy findBy) {

            if (!"".equals(findBy.id())) {
                String id = findBy.id();
                if (id.contains(YEAR_ID)) {
                    id = id.replace(YEAR_ID, yearId);
                    return By.id(id);
                } else if (id.contains(MONTH_ID)) {
                    id = id.replace(MONTH_ID, monthId);
                    return By.id(id);
                } else if (id.contains(DAY_ID)) {
                    id = id.replace(DAY_ID, dayId);
                    return By.id(id);
                }
            }

            return super.buildByFromShortFindBy(findBy);
        }

    }

}

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Can we reuse a Gson object?

分類Dev

Reuse a property value as a variable within the same object

分類Dev

How to reuse this ViewController code in an object oriented way?

分類Dev

Having issues defining Xpath while using Page object in Selenium WebDriver testing

分類Dev

Reuse Selenium WebDriver Session When Restarting Java Application

分類Dev

reuse contact form 7 submission on thank you page

分類Dev

Cucumber + Selenium Webdriver + Page-Objectでブラウザを再起動する方法は?

分類Dev

Next Page Selenium with Scrapy not working

分類Dev

Verify the response code of the page with Capybara and Selenium

分類Dev

How to disable javascript on a specific page in Selenium?

分類Dev

Selenium how to manage wait for page load?

分類Dev

Xrm.Page object hierarchy

分類Dev

Object Type not recognized in aspx page

分類Dev

Spring JPA: ManyToMany relationship not stored in database when reuse the property to store another object

分類Dev

Reuse of functions

分類Dev

Selenium (Java) Get FULL text from shortened displayed on page

分類Dev

Selenium ChromeDriver C# how to navigate page before it loads?

分類Dev

Selenium driver's page source different than browser

分類Dev

Selenium, Get Elements By Xpath - Only grab last 60 elements on page

分類Dev

How to get webDriver to wait for page to load (C# Selenium project)

分類Dev

stop loading page selenium as soon as pyqt button is clicked

分類Dev

How to get false if element is not present on page in Selenium webdriver

分類Dev

How to web-scrape multiple page with Selenium (Python)

分類Dev

Selenium doesn't get all the href from a web page

分類Dev

Despite Selenium WebDriver only parts of the page are being parsed

分類Dev

Common Step Definitions in Specflow with Page Object Model

分類Dev

How to bind an entity object on a Detail page

分類Dev

Page Factory DesignとPage Object Modelを使用したテストは、SeleniumとJavaを使用してブラウザーの2つのインスタンスを開きます

分類Dev

Vuex state is null in mutation after page refresh instead predefined object

Related 関連記事

  1. 1

    Can we reuse a Gson object?

  2. 2

    Reuse a property value as a variable within the same object

  3. 3

    How to reuse this ViewController code in an object oriented way?

  4. 4

    Having issues defining Xpath while using Page object in Selenium WebDriver testing

  5. 5

    Reuse Selenium WebDriver Session When Restarting Java Application

  6. 6

    reuse contact form 7 submission on thank you page

  7. 7

    Cucumber + Selenium Webdriver + Page-Objectでブラウザを再起動する方法は?

  8. 8

    Next Page Selenium with Scrapy not working

  9. 9

    Verify the response code of the page with Capybara and Selenium

  10. 10

    How to disable javascript on a specific page in Selenium?

  11. 11

    Selenium how to manage wait for page load?

  12. 12

    Xrm.Page object hierarchy

  13. 13

    Object Type not recognized in aspx page

  14. 14

    Spring JPA: ManyToMany relationship not stored in database when reuse the property to store another object

  15. 15

    Reuse of functions

  16. 16

    Selenium (Java) Get FULL text from shortened displayed on page

  17. 17

    Selenium ChromeDriver C# how to navigate page before it loads?

  18. 18

    Selenium driver's page source different than browser

  19. 19

    Selenium, Get Elements By Xpath - Only grab last 60 elements on page

  20. 20

    How to get webDriver to wait for page to load (C# Selenium project)

  21. 21

    stop loading page selenium as soon as pyqt button is clicked

  22. 22

    How to get false if element is not present on page in Selenium webdriver

  23. 23

    How to web-scrape multiple page with Selenium (Python)

  24. 24

    Selenium doesn't get all the href from a web page

  25. 25

    Despite Selenium WebDriver only parts of the page are being parsed

  26. 26

    Common Step Definitions in Specflow with Page Object Model

  27. 27

    How to bind an entity object on a Detail page

  28. 28

    Page Factory DesignとPage Object Modelを使用したテストは、SeleniumとJavaを使用してブラウザーの2つのインスタンスを開きます

  29. 29

    Vuex state is null in mutation after page refresh instead predefined object

ホットタグ

アーカイブ