2015-11-06 64 views
1

我是自動化測試的新手,m對assert和verify很困惑。因爲我使用TestNG,根據我的研究,我開始知道在webdriver中,我們沒有驗證,我們有硬性和軟性的斷言。但是當我搜索它時,我會得到所有不同的答案。我無處可以找到一個詳細的例子。 對於軟斷言,我看到有人使用'customverification',但是當我嘗試在我的程序中寫入時,出現錯誤,要求創建類或接口。 有人可以幫助我在這個。我正在通過互聯網學習,所以很難得到正確的答案。 感謝Webdriver的硬和軟斷言

package revision; 

import org.openqa.selenium.By; 
import org.openqa.selenium.WebDriver; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.testng.Assert; 
import org.testng.annotations.Test; 


public class Six { 
WebDriver driver=new FirefoxDriver(); 


    @Test 
    public void SandyOne() { 
    driver.get("file:///C:/Users/Sandeep%20S/Desktop/Test.html"); 
Assert.assertTrue(IsElementPresent(By.xpath("//input[@id='custom']")), "tab was missing"); 
driver.findElement(By.xpath("//input[@id='custom']")).sendKeys("abcd"); 
    System.out.println("1st program"); 

    System.out.println("blah 1"); 
    System.out.println("blah 2"); 
    } 


    public boolean IsElementPresent(By by) { 
    try { 
     driver.findElements(by); 
     return true; 
    } catch (org.openqa.selenium.NoSuchElementException e) { 
     return false; 
    } 
} 
} 
+0

你有什麼試過,結果是什麼?正如你在學校所做的那樣,請展示你的工作。 :)這是在SO上獲得問題的過程中的一部分。這對你很有幫助,因爲它會迫使你調查自己的問題並思考問題。這也向讀者證明你做了功課,並做出了合理的嘗試來回答你自己的問題。第三,它可以幫助讀者找到和診斷問題,爲您提供更好的答案,減少浪費時間。 – JeffC

回答

1

如果您有未通過測試的斷言將被停止,其中用於驗證測試將繼續並會記錄錯誤。

理想情況下,每次測試只有一個斷言(例如正確的頁面已經加載),並且在這種情況下驗證將用於檢查該頁面上的信息。 因此,如果沒有正確的頁面加載,檢查頁面上的東西是沒有意義的。

你可以得到一個想法和一個視覺例子here

+0

雖然這可能在理論上回答這個問題,[這將是更可取的](/ meta.stackoverflow.com/q/8259)在這裏包括答案的基本部分,並提供鏈接以供參考。 – JeffC

+1

@JeffC我的不好,我編輯了我的答案,並且還添加了何時以及爲什麼要使用它們。感謝您的提醒。 – Cosmin

1

您的測試可能失敗在這裏:

Assert.assertTrue(IsElementPresent(By.xpath("//input[@id='custom']")), "tab was missing"); 

因爲IsElementPresent回報false。避免這種情況的一種方法是:

try { 
    Assert.assertTrue(IsElementPresent(By.xpath("//input[@id='custom']")), "tab was missing"); 
    driver.findElement(By.xpath("//input[@id='custom']")).sendKeys("abcd"); 
} 
catch (AssertionError ae) { 
    //ignore 
} 

但是,捕獲錯誤是相當難看的代碼。更好的方法是使用WebDriver.findElements(By by)並檢查結果列表是否爲空。

0

硬聲明:一旦發現聲明失敗,測試執行就會停止。

軟斷言:即使發現斷言失敗,測試執行也會繼續。

例如您有3個斷言語句Assert1,Assert2,Assert3

現在,如果Assert2在硬斷言失敗時將終止測試。 在軟斷言的情況下,它將移動到測試中的下一步,然後終止。

您需要實例軟斷言爲:

SoftAssertions softAssertion =新SoftAssertions();

softAssertion.assertTrue(條件,消息)

在你給出的代碼片斷很難斷言是有道理的,因爲直到輸入框中找到你不能移動到下一個步驟發送文本。

0

在給定的例子中,你不需要斷言元素的存在。如果它缺少findElement方法會拋出一個錯誤,你會知道它不在那裏。

如果您有元素的ID,請使用它代替xpath。這將使代碼的可讀性更強,更快:

driver.findElement(By.Id("custom")).sendKeys("abcd"); 

而不是直接調用findElement方法,建議使用PageObject模式,並挑選註釋元素,請參閱PageFactory

public class TestPage { 
    @FindBy(id = "custom") 
    WebElement custom; 

    private WebDriver driver; 

    public TestPage (WebDriver driver) { 
     this.driver = driver; 
     PageFactory.initElements(driver, this); 
    } 

    public TestPage inputCustom(String txt) { 
     custom.sendKeys(txt); 
     return this; 
    } 
} 

@Test 
public void SandyOne() { 
    // ... 
    TestPage page = new TestPage(driver); 
    page.inputCustom("abcd"); 
    // ... 
}