2017-05-04 26 views
-3

我是新來的硒。 我需要檢查系統中按鈕的可用性,並需要使用AssertEquals將其標記爲通過並失敗。用TestNG檢查按鈕

請幫幫我。

@Test 
public void sellercheck() throws InterruptedException 
{ 
     Thread.sleep(2000); 
     driver.findElement(By.id("UserEvent")).click(); 
     //String r=Read.getvalue().get(0); 
     //select the seller 
     driver.findElement(By.id("LegacyNumberCriterion")).sendKeys("123456"); 
     driver.findElement(By.id("SuperUse")).click(); 
     System.out.println("seller number entered");   
     try 
     { 
     if(driver.findElements(By.id("OrganizationBranchId")).size()!=0) 
     { 
      driver.findElement(By.id("button1")).click(); 
     } 
     else 
     { 
      System.out.println("The button is not available for the seller"); 
     } 
     } 
     catch(NoSuchElementException e) 
     { 
     System.out.println("Element does not exist!"); 
     } 
    } 
+0

請提供更多信息:1.您的確切測試步驟是什麼? 2.什麼對你有用?顯示代碼。 3.你卡在哪裏? 4.提供錯誤堆棧跟蹤。 5.提供相關的HTML DOM。 – DebanjanB

+0

1.如果有按鈕可用,測試用例已通過,或者如果失敗,則需要使用ID登錄後進行驗證。 – Bitz

+0

我已添加代碼 – Bitz

回答

1

您可以從以下2個解決方案,適合你的選擇:

1]代碼來檢查元素存在或硒的webdriver不使用斷言將是這樣的:

assertTrue(!isElementPresent(By.id("id of button"))); 

2]該斷言驗證DOM中沒有匹配的元素並返回零值,所以斷言在元素不存在。如果它存在,它也會失敗。

Assert.assertEquals(0, driver.findElement(By.id("id of button")).size()); 

試試這個解決方案,讓我知道它是否工作。

+0

使用硒2.53,isElementPresent不來。 – Bitz

0

嘗試這段代碼有一些簡單的調整,以自己的代碼:

WebDriver driver; 
@Test 
public void sellercheck() throws InterruptedException 
{ 
    Thread.sleep(2000); 
    driver.findElement(By.id("UserEvent")).click(); 
    //String r=Read.getvalue().get(0); 
    //select the seller 
    driver.findElement(By.id("LegacyNumberCriterion")).sendKeys("123456"); 
    driver.findElement(By.id("SuperUse")).click(); 
    System.out.println("seller number entered"); 
    try 
    { 
     if(driver.findElements(By.id("OrganizationBranchId")).size()!=0) 
     { 
      driver.findElement(By.id("button1")).click(); 
     } 
     else 
     { 
      System.out.println("The button is not available for the seller"); 
     } 
    }catch(NoSuchElementException e) 
    { 
      System.out.println("Element does not exist!"); 
    } 
} 

讓我知道這對你的作品或更新我看到錯誤。

+0

你沒有改變任何東西。請花一分鐘來總結一下你的代碼的作用,它與OP代碼的區別,以及它如何回答這個問題。 – JeffC