TestNG также позволяет делать дополнительные проверки в средине теста с помощью Asserts. Asserts являются наиболее популярными и действенными способами при написании Selenium Webdriver тестов. Например, довольно часто приходится делать проверки на наличие элемента. Для этого используют вставки Asserts.
Наиболее используемыми Assert вставками являются:
1) Assert.assertTrue() и Assert.assertFalse()
Используем пример предыдущей статьи.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
package automationFramework; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.Test; import org.testng.annotations.BeforeMethod; import org.testng.annotations.AfterMethod; public class FirstTestCase { public WebDriver driver; @Test public void main() { // Find the element that's ID attribute is 'user_login' (Username) // Enter Username on the element found by above desc. driver.findElement(By.id("user_login")).sendKeys("subscriber"); // Find the element that's ID attribute is 'user_pass' (Password) // Enter Password on the element found by the above desc. driver.findElement(By.id("user_pass")).sendKeys("2016subscriberpasssword2016ok"); // Now submit the form. WebDriver will find the form for us from the // element driver.findElement(By.id("wp-submit")).click(); /* if (driver.findElement(By.id("profile-page")).isDisplayed()) { // Print a Log In message to the screen System.out.println("Login successfully."); }*/ // instead of IF operation we can use Assert for verification that we are at the user profile page Assert.assertTrue(driver.findElement(By.id("profile-page")).isDisplayed()); WebElement source = driver.findElement(By.xpath("//li[@id='wp-admin-bar-my-account']/a[@class='ab-item']")); // Assertion on correctness of user name Assert.assertEquals("How are you, subscriber subscriber?", source.getText()); } @BeforeMethod public void beforeMethod() { // Create a new instance of the Firefox driver driver = new FirefoxDriver(); // Put a Implicit wait, this means that any search for elements on the // page could take the time the implicit wait is set for before throwing // exception driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); // Launch the Wordpress Admin site driver.get("http://autoqa.pp.ua/wp-admin"); } @AfterMethod public void afterMethod() { // Close the driver driver.quit(); } } |
Обратите внимание, что Assert true выражение “заваливает” тест и прекращает его выполнение, если результат является false. Assert.assertFalse() работает противоположно к Assert.assertTrue(). Это означает, что если Вы хотите, чтобы тест выполнялся только в том случае, когда определенный элемент не отображается на странице.
2) Assert.assertEquals()
1 2 3 4 5 |
WebElement source = driver.findElement(By.xpath("//li[@id='wp-admin-bar-my-account']/a[@class='ab-item']")); // Assertion on correctness of user name Assert.assertEquals("How are you, subscriber subscriber?", source.getText()); |
Она работает таким же образом, как и assert true и assert fail, и останавливает выполнение теста, если сравниваемые значения не совпадают.