如何在 Selenium POM 中增加?

我试图通过执行 for 循环在输入标签上增加两次来选择 Selenium POM 中相同项目的两个数量,但我的解决方案似乎不起作用。如何使用 POM 递增两次?


这是我存储页面对象的文件:


package pageObjects;


import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.ui.Select;


public class TTPStorePage {


    WebDriver driver;


    public TTPStorePage(WebDriver driver) {

        this.driver = driver;

    }


    By size= By.id("size");

    By quantity= By.id("quantity_5cb788738ee07");

    By reset= By.className("reset_variations");

    By submit=By.cssSelector("button[type='submit']");

    By remove=By.xpath("//a[contains(@data-gtm4wp_product_id,'TS-TTP']");

    By contents=By.className("cart-contents");



    public void selectSize(int index) {

        Select drop = new Select(driver.findElement(size));

        drop.selectByIndex(index);

    }


    // The problem child.

    public void quantityItem() {

        for(int i=0;i<2;i++) {

            driver.findElement(quantity).sendKeys(Keys.ARROW_UP);

        }

    }


    public WebElement resetItems() {

        return driver.findElement(reset);

    }



    public WebElement submitButton() {

        return driver.findElement(submit);

    }


    public WebElement removeItem() {

        return driver.findElement(remove);

    }


    public WebElement cartContents() {

        return driver.findElement(contents);

    }


}

这是我存储测试用例的文件。这是我遇到大部分问题的地方。我试图弄清楚如何正确地将我的项目增加两个。


package SimpleProgrammer;


import java.io.IOException;

import org.testng.annotations.Test;

import resources.Base;

import pageObjects.TTPProductPage;

import pageObjects.TTPStorePage;



    }


}


波斯汪
浏览 68回答 2
2回答

红糖糍粑

它是 INPUT 字段,只需先清除输入字段,然后为 sendKeys 提供值。请取唯一的名称 attr,您的 ID attrs 不是唯一的。WebElement ele=driver.findElement(By.name("quantity"));ele.clear();ele.sendKeys("2");

天涯尽头无女友

我查看了您尝试访问的网页。您尝试TTPStorePage通过定位器在类中访问的微调器quantity具有动态 ID。每次加载页面时它都会更改。您必须更改定位器策略。尝试使用以下定位器之一作为数量。CSS选择器:By quantity = By.cssSelector("div.quantity > input");XPath:By quantity = By.xpath("//div[@class='quantity']/input");此外,quantityItem您不需要 for 循环的 in 方法,因为您可以将值直接设置为您想要的值,sendKeys因为它是一个input元素。尝试这个public void quantityItem() {&nbsp; &nbsp; driver.findElement(quantity).clear();&nbsp; &nbsp; driver.findElement(quantity).sendKeys("3");&nbsp; &nbsp; //pressing up arrow twice would make the spinner value 3}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java