2016-02-17 37 views
0

在硒webdriver我想用java/if條件。每個步驟都需要點擊,並且需要執行一次。使用Number_Select.NumberRandom(driver,2).click()的條件; = numberRandom中if else語句的選擇2。例如,所以我的問題是一旦測試用例運行它只會點擊一個數字,假設點擊2個隨機數字。請告知如何在硒webdriver中使用if/else條件與onclick?

的TestCase:

@Test(description = "Login Page Test" , enabled = true) 
    public void faaaa() throws Exception { 
     try{ 
      driver = new FirefoxDriver(); 
      SignIn_Action.Execute(driver); 
      Menu_Select.Menu_Selection(driver, 2).click(); 
      MenuLottery_Select.Menu_Selection(driver, 1).click(); 
      MenuSubTab_Select.MenuSubTab_1(driver, 3).click(); 
      Number_Select.NumberRandom(driver, 2).click(); 
      //SignIn_Action.Logout_Session(driver); 
      //Log.info("Login Successful"); 
     }catch (Exception e){ 

      System.out.println("Test 1"); 
      //Log.error(e.getMessage()); 
      throw (e); 
     } 

    } 

Number_Select:

public static WebElement NumberRandom(WebDriver driver, int selection) 
      throws Exception { 

     if (selection == 1) { 
      Thread.sleep(1500); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl/dd/i[8]")); 
     } else if (selection == 2) { 
      Thread.sleep(1500); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]")); 
     } else if (selection == 3) { 
      Thread.sleep(1500); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]")); 
     } else if (selection == 4) { 
      Thread.sleep(1500); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[4]/dd/i[1]")); 
     } else if (selection == 5) { 
      Thread.sleep(1500); 
      element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[3]/dd/i[5]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[4]/dd/i[1]")); 
      element = driver.findElement(By 
        .xpath("//*[@id='lottery']/div[3]/dl[5]/dd/i[9]"));//*[@id="lottery"]/div[3]/dl[1]/dd/i[1] 
     } else { 
      Log.error("Unable to select product list"); 
      System.out.println("Unable select product list"); 
     } 

     return element; 
    } 
+0

不太明白你想要什麼?然而,我很困惑你在每種情況下設置元素的方式。只有最後一個元素是有效的,可以返回。 –

回答

2

NumberRandom將返回只有一個WebElement。即使您每次覆蓋最後一個值element,並且最後一個返回到faaaa()時,都在同一個if塊中查找多個WebElement。如果你想點擊的所有元素,你需要將click()插入的方法

else if (selection == 2) { 
    Thread.sleep(1500); 
    element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]")).click(); 
    element = driver.findElement(By.xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]")).click(); 
} 

作爲一個側面說明,你應該避免使用Thread.sleep。如果你想確保元素是可點擊使用顯式的等待和expected conditions

WebDriverWait wait = new WebDriverWait(driver, 10); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lottery']/div[3]/dl[1]/dd/i[8]"))).click(); 
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id='lottery']/div[3]/dl[2]/dd/i[3]"))).click(); 

這將等待長達10秒的元素點擊它之前被點擊。

+0

感謝您的幫助和信息。好工作兄弟 – user1210826