【问题标题】:What is the use of @FindBys and @FindAll@FindBys 和 @FindAll 有什么用
【发布时间】:2018-04-23 13:51:02
【问题描述】:

为了低调,我使用了this

另外,@FindBys 有 AND 条件,而 @FindAll 有 OR 条件,我在下面的代码中尝试了这个,

@FindBys(

        {
            @FindBy(name="username"),
            @FindBy(name="password")

        }


        )

List<WebElement> totWeAnd;

@FindAll(

        {

            @FindBy(name="username"),
            @FindBy(name="password")

        }

        )
List<WebElement> totWeOr;

正如预期的那样,totWeAnd 返回了 0 个 WebElements,totWeOr 返回了 2 个 WebElements。 但同样的事情可以通过使用 FinBy(xpath="") 来实现,如下所示,

    @FindBy(xpath="//input[@name='username' and @name='password']")
    List<WebElement> totWE_And;

    @FindBy(xpath="//input[@name='username' or @name='password']")
    List<WebElement> totWE_OR;

那么@FindBys、@FindAll 有什么用,什么时候应该使用呢?

【问题讨论】:

    标签: java selenium selenium-webdriver pageobjects


    【解决方案1】:

    文章中的逻辑不正确。每个FindBy mentioned in the [FindBys][1] or [FindAll][1] annotation 指的是与标准匹配的特定元素,而不是标准的“或”和“与”组合。

    @FindBys - 返回与每个 FindBy 返回的元素序列匹配的元素链。

    @FindAll - 返回与每个 FindBy 返回的每个元素匹配的所有元素。

    最简单的方法是使用一些代码进行测试。检查链接的源代码,尤其是findElements() 方法。

    下面是一个用于测试的简单 HTML

    <div id='level1elem1' name='LEVEL1 ELEMENT1'>
        <div id='level2elem1' name='LEVEL2 ELEMENT1'>
        </div>
        <div id='level2elem2' name='LEVEL2 ELEMENT2'>
        </div>
    </div>
    

    PageObject 类。引用元素变量前的 cmets。

    public class FindPageObject {
    
        private WebDriver driver;
    
        //This will match an element (id of level2elem1) inside a parent element (id of level1elem1).
        //Refer to test - testFindBys.
        //Return a POSITIVE match.
        @FindBys({
            @FindBy(id="level1elem1"),
            @FindBy(id="level2elem1")
        })
        private WebElement singleFindBys;
    
        //This NEEDS TO MATCH an element (id of level1elem1) inside a parent element (id of level2elem1).
        //Refer to test - testFindBysRev.
        //Return a NoSuchElementException as element heirarchy is not present.
        @FindBys({
            @FindBy(id="level2elem1"),
            @FindBy(id="level1elem1")
        })
        private WebElement singleFindBysRev;
    
        //This will match the first element that matches any of the 3 criterion.
        //Refer to test - testFindByAll.
        //Return a POSITIVE match of top most element (id of level1elem1).
        @FindAll({
            @FindBy(id="level1elem1"),
            @FindBy(id="level2elem1"),
            @FindBy(id="level2elem2")
        })
        private WebElement singleFindAllUI;
    
        //This will match the first element that matches any of the 3 criterion.
        //In this case the inner elemnts FindBy are placed first.
        //Refer to test - testFindByAllRev.
        //Return a POSITIVE match of top most element (id of level2elem2).
        @FindAll({
            @FindBy(id="level2elem2"),
            @FindBy(id="level2elem1"),
            @FindBy(id="level1elem1")
        })
        private WebElement singleFindAllUIRev;
    
        //This will match all the elements for each criterion.
        //Refer to test - testFindByAllMultiple.
        //Return a POSITIVE match of a list of all the elements of size 3.
        @FindAll({
            @FindBy(id="level1elem1"),
            @FindBy(id="level2elem1"),
            @FindBy(id="level2elem2"),
        })
        private List<WebElement> multipleFindAll;
    
        //This will match all the elements for each criterion.
        //Element(id of level1elem1 & name LEVEL1 ELEMENT1) will be returned twice as it matches the first and last findby.
        //Refer to test - testFindByAllMultipleDuplicate.
        //Return a POSITIVE match of a list of all the elements of size 4.
        @FindAll({
            @FindBy(id="level1elem1"),
            @FindBy(id="level2elem1"),
            @FindBy(id="level2elem2"),
            @FindBy(name="LEVEL1 ELEMENT1"),
        })
        private List<WebElement> multipleFindAllDuplicate;
    
        //This will match all the elements that satisfy the xpath.
        //Though it has 4 matchers returns only 3 elements.
        //Refer to test - testXpathAlls.
        //Return a POSITIVE match of a list of all the elements of size 3.
        @FindBy(xpath="//div[@id='level1elem1' or @id='level2elem2' or @id='level2elem1' or @name='LEVEL1 ELEMENT1']")
        private List<WebElement> xpathAlls;
    
    
        public FindPageObject(WebDriver driver) {
            this.driver = driver;
            PageFactory.initElements(driver, this);
        }
    
    
        public String getSingleFindBys() {
            return singleFindBys.getAttribute("name");
        }
    
        public String getSingleFindBysRev() {
            return singleFindBysRev.getAttribute("name");
        }
    
        public String getSingleFindAll() {
            return singleFindAllUI.getAttribute("name");
        }
    
        public String getSingleFindAllRev() {
            return singleFindAllUIRev.getAttribute("name");
        }
    
        public int getSizeMultiple() {
            return multipleFindAll.size();
        }
    
        public int getSizeMultipleDuplicate() {
            return multipleFindAllDuplicate.size();
        }
    
        public int getSizeMultipleXpaths() {
            return xpathAlls.size();
        }
    }
    

    junit 测试类。

    public class FindTest {
    
        private static WebDriver driver;
    
    
        @Test
        public void testFindBys() {
            FindPageObject fpo = new FindPageObject(driver); 
            assertEquals("LEVEL2 ELEMENT1", fpo.getSingleFindBys());
        }
    
        @Test
        public void testFindBysRev() {
            FindPageObject fpo = new FindPageObject(driver); 
            thrown.expect(NoSuchElementException.class);
            fpo.getSingleFindBysRev();
        }
    
        @Test
        public void testFindByAll(){
            FindPageObject fpo = new FindPageObject(driver); 
            assertEquals("LEVEL1 ELEMENT1", fpo.getSingleFindAll());
        }
    
        @Test
        public void testFindByAllRev(){
            FindPageObject fpo = new FindPageObject(driver); 
            assertEquals("LEVEL2 ELEMENT2", fpo.getSingleFindAllRev());
        }
    
        @Test
        public void testFindByAllMultiple(){
            FindPageObject fpo = new FindPageObject(driver); 
            assertEquals(3, fpo.getSizeMultiple());
        }
    
        @Test
        public void testFindByAllMultipleDuplicate(){
            FindPageObject fpo = new FindPageObject(driver); 
            assertEquals(4, fpo.getSizeMultipleDuplicate());
        }
    
        @Test
        public void testXpathAlls(){
            FindPageObject fpo = new FindPageObject(driver); 
            assertEquals(3, fpo.getSizeMultipleXpaths());
        }
    
        @Rule
        public ExpectedException thrown = ExpectedException.none();
    
        @BeforeClass
        public static void setUp() {
            System.setProperty("webdriver.chrome.driver",
                    "Path to chromedriver");
    
            ChromeOptions options = new ChromeOptions();
            options.addArguments("--start-maximized");
    
            driver = new ChromeDriver(options);
            driver.get("Local path to html");
        }
    
        @AfterClass
        public static void tear() {
            driver.quit();
        }
    }
    

    关于用法,从未使用过,因为创建 css 或 xpath 更容易。查询页面的次数越多,性能也越好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多