Problem: Thiếu handle exception cho Thread.sleep(2000);
Reason: Thread.sleep(long milisecond) là 1 method sẽ throws Check Exception, do đó, code client (nơi sử dụng method trên) sẽ phải handle exception.
Fix:
- Cách 1: throws exception trên method signature.
@Test
public void verifyHomepageTitle() throws InterruptedException {
....
}- Cách 2: dùng try-catch
@Test
public void verifyHomepageTitle() {
WebDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.navigate().to("trang_web_của_bạn");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
String expectedTitle = "Text gì cũng được";
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, expectedTitle);
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
driver.quit();
}
Em nghĩ không nên sử dụng Thread.sleep() mà thay bằng Explicit Wait, như vậy sẽ tránh được những rủi ro liên quan đến mạng chậm.
WebDriverWait wait=new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[text()='Text gì cũng được']")));Ngoài ra nếu vẫn muốn sử dụng Thread.sleep() thì phải xử lý exception cho sleep()
public void verifyHomepageTitle() throws InterruptedException{Đáp án đúng rồi nhé. ^^
Yay, hóa ra còn cách try-catch, e sẽ đọc thêm cái này ạ. Thank u!