2019년 12월 5일 목요일

Selenium implicitly-wait and explicitly-wait

implicitly-wait : 브라우져 전체 요소가 로드될 때 까지 기다린다.
explicitly-wait : 특정 요소가 로드될 때 까지 기다린다.

implicitly wait
 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
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class WaitTest {

 private WebDriver driver;
 private String baseUrl;
 private WebElement element;

 @BeforeMethod
 public void setUp() throws Exception {
  driver = new FirefoxDriver();
  baseUrl = "http://www.google.com";
  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
 }

 @Test
 public void testUntitled() throws Exception {
  driver.get(baseUrl);
  element = driver.findElement(By.id("lst-ib"));
  element.sendKeys("Selenium WebDriver Interview questions");
  element.sendKeys(Keys.RETURN);
  List<WebElement> list = driver.findElements(By.className("_Rm"));
  System.out.println(list.size());
 }

 @AfterMethod
 public void tearDown() throws Exception {
  driver.quit();
 }
}


Explicitly wate
 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
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class ExpectedConditionExample {
 // created reference variable for WebDriver
 WebDriver driver;

 @BeforeMethod
 public void setup() throws InterruptedException {
  // initializing driver variable using FirefoxDriver
  driver=new FirefoxDriver();
  // launching gmail.com on the browser
  driver.get("https://gmail.com");
  // maximized the browser window
  driver.manage().window().maximize();
  driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
 }

 @Test
 public void test() throws InterruptedException {
  // saving the GUI element reference into a "element" variable of WebElement type
  WebElement element = driver.findElement(By.id("Email"));
  // entering username
  element.sendKeys("dummy@gmail.com");
  element.sendKeys(Keys.RETURN);
  // entering password
  driver.findElement(By.id("Passwd")).sendKeys("password");
  // clicking signin button
  driver.findElement(By.id("signIn")).click();
  // explicit wait - to wait for the compose button to be click-able
  WebDriverWait wait = new WebDriverWait(driver,30);
  wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[contains(text(),'COMPOSE')]")));
  // click on the compose button as soon as the "compose" button is visible
  driver.findElement(By.xpath("//div[contains(text(),'COMPOSE')]")).click();
 }

 @AfterMethod
 public void teardown() {
  // closes all the browser windows opened by web driver
  driver.quit();
 }
}


Fluent wait
1
2
3
4
5
6
7
8
9
Wait wait = new FluentWait(WebDriver reference)
.withTimeout(timeout, SECONDS)
.pollingEvery(timeout, SECONDS)
.ignoring(Exception.class);

WebElement foo = wait.until(new Function<WebDriver, WebElement>() {
 public WebElement applyy(WebDriver driver) {
 return driver.findElement(By.id("foo"));
}});

댓글 없음:

댓글 쓰기