Vì một số lý do như có quá nhiều thông báo...nên mình sẽ không dùng trang web của adayroi nữa mà chuyển qua web của guru99 để thực hành.
Testcase của bài tập ngày hôm nay như sau:
package demo_thuchanh;
import static org.testng.Assert.assertEquals;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
public class TestCase1 {
private WebDriver driver;
private String url;
@BeforeTest
public void setUp() throws Exception {
System.setProperty("webdriver.chrome.driver","C:\\chrome\\chromedriver.exe");
driver = new ChromeDriver();
url = "http://live.guru99.com/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void TestCase() throws Exception {
// 1. Go to http://live.guru99.com
driver.get(url);
// 2. Click on Mobile menu
driver.findElement(By.linkText("MOBILE")).click();
// 3. In the list of all mobile , read the cost of Sony Xperia mobile (which is $100)
String gia_XPeria = driver.findElement(By.cssSelector("#product-price-1 > span.price")).getText();
// 4. Click on Sony Xperia mobile
driver.findElement(By.id("product-collection-image-1")).click();
// 5. Read the XPeria mobile price from details page
String gia_Detail = driver.findElement(By.cssSelector("span.price")).getText();
// Product price in list and details page should be equal ($100)
try {
assertEquals(gia_XPeria, gia_Detail);
} catch (Exception e) {
e.printStackTrace();
}
}
@AfterTest
public void tearDown() throws Exception {
driver.quit();
}
}
Hãy nhìn vào code, bạn sẽ thấy cú pháp @BeforeTest, @Test và @AfterTest.
- Những thứ setup cần thiết, chúng ta sẽ nhét vào BeforeTest
- Các bước thực hiện testcase, thì chúng ta sẽ cho vào Test
- Sau cùng thì sẽ cho vào AfterTest, kiểu như close() hay quit() :D
Đây là kết quả sau khi chạy testcase này
Hẹn gặp các bạn ở bài tiếp theo.

0 comments:
Post a Comment