2017-03-12 8 views
0

私は3番目のドロップダウンの値が一定で、2番目のドロップダウンの値を変更しなければならないこの変更のうち、私はウェブページ上の店舗の座標を取得します。最初と3番目のドロップダウンを一定に保ち、2番目のドロップダウンの値をforループを使って変更しています。価値は変化していますが、ウェブページ上の座標は変化していませんが。ページ上の座標の値は、最初の選択と同じままです。以下、私はフィールドおよびメソッドにすべてを分離パブリック静的な無効メイン(文字列[]引数)予め{古い要素参照例外 - リフレッシュ後にドロップダウンの値を変更しようとしています

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

2番目のドロップダウンの選択ごとに店舗の値の値を変更する必要があります。私のコードでは、以下の行を使用して数値ストアをカウントしようとしています。int = driver.findElements(By.xpath( ".// div [@ id = 'storeLocator'] // [text()= 'Locate Us' ]"))。サイズ();ここでドロップダウンの値は変わりますが、店舗の座標は同じです –

+0

これはどのように動作するのですか、ページを更新した後で再度要素を見つける必要があります。 – lauda

答えて

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

次のステップでは、ページオブジェクトからテストロジックを分離することです。

関連する問題