我正在进行3次下拉,其中第一次和第三次下拉的值是恒定的,我必须更改第二次下拉的值,并根据此更改我获得网页上商店的坐标。我保持第一个和第三个下拉不变,并使用for循环更改第二个下拉值。虽然价值在变化,但网页上的坐标并没有改变。页面上坐标的值与第一次选择的值相同。下面是我写的代码
public static void main(String [] args){
System.setProperty("webdriver.chrome.driver",
"D:\\SELONew\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("http://www.motherdairy.com/StoreLocator");
// ****************** Selecting State Drop Down
Select stateDropDown = new Select(driver.findElement(By
.xpath(".//*[@id='drpState']")));
stateDropDown.selectByVisibleText("Delhi");
// ******************* Selecting City Drop Down
Select cityDropDown = new Select(driver.findElement(By.id("drpCity")));
int citydrpdwnsize = cityDropDown.getOptions().size();
for (int i = 1; i <= citydrpdwnsize; i++) {
cityDropDown.selectByIndex(i);
Select product = new Select(driver.findElement(By.id("drpBrand")));
product.selectByVisibleText("Mother Dairy");
int s = driver
.findElements(
By.xpath(".//div[@id='storeLocator']//a[text()='Locate Us']"))
.size();
System.out.println(s);
driver.navigate().refresh();
stateDropDown.selectByVisibleText("Delhi");
}
提前致谢:)
答案 0 :(得分:0)
将它们全部分成字段和方法应该是正确的做法。 不知道为什么你需要刷新网站,但有了它应该是这样的:
public class TestClass {
private static WebDriver driver;
private static WebDriverWait wait;
@FindBy(id = "drpState")
private WebElement stateDropDown;
@FindBy(id = "drpCity")
private WebElement cityDropDown;
@FindBy(id = "drpBrand")
private WebElement brandDropDown;
@BeforeClass
public static void setUp() {
System.setProperty("webdriver.chrome.driver",
"D:\\SELONew\\chromedriver.exe");
driver = new ChromeDriver();
wait = new WebDriverWait(driver, 5);
driver.manage().window().maximize();
}
@Test
public void testMethod() {
driver.navigate().to("http://www.motherdairy.com/StoreLocator");
PageFactory.initElements(driver, this);
// ****************** Selecting State Drop Down
selectTextFromDropDown(stateDropDown, "Delhi");
// ******************* Selecting City Drop Down
waitForListToHaveEnoughElements(cityDropDown, 1);
int cityDropDownSize = getOptionsFromDropDown(cityDropDown).size();
for (int i = 0; i < cityDropDownSize; i++) {
new Select(cityDropDown).selectByIndex(i);
selectTextFromDropDown(brandDropDown, "Mother Dairy");
int size = driver
.findElements(By.xpath(".//div[@id='storeLocator']//a[text()='Locate Us']"))
.size();
System.out.println(size);
driver.navigate().refresh();
//Initialize all elements one more time:
PageFactory.initElements(driver, this);
wait.until(ExpectedConditions.visibilityOf(stateDropDown));
wait.until(ExpectedConditions.visibilityOf(cityDropDown));
wait.until(ExpectedConditions.visibilityOf(brandDropDown));
selectTextFromDropDown(stateDropDown, "Delhi");
waitForListToHaveEnoughElements(cityDropDown, 1);
//It is a good place for some assertions.
}
}
private void selectTextFromDropDown(WebElement forSelect, String text) {
Select select = new Select(forSelect);
select.selectByVisibleText(text);
}
private List<WebElement> getOptionsFromDropDown(WebElement forSelect) {
return new Select(forSelect).getOptions();
}
private void waitForListToHaveEnoughElements(WebElement forList, int size) {
wait.until((Predicate<WebDriver>) driver -> {
List<WebElement> options = getOptionsFromDropDown(forList);
return options.size() > size;
});
}
}
下一步是将测试逻辑与页面对象分开。