-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjUnitHomeWork.java
More file actions
84 lines (67 loc) · 2.93 KB
/
jUnitHomeWork.java
File metadata and controls
84 lines (67 loc) · 2.93 KB
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
83
84
/**
* Created by adudyak on 6/8/2016.
*/
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.MarionetteDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import java.io.IOException;
import java.util.ArrayList;
public class JUnitHomeWork {
// Create object of interface WebDriver.
WebDriver driver;
//Annotation that sends to JUnit message that this method should be executed BEFORE test method
@Before
public void setUp() {
/*
* Initialize object driver. According to internal realization of selenium
* we can initialize driver object as
* WebDriver driver = new FirefoxDriver();
* or
* WebDriver driver = new ChromeDriver();
* not
* WebDriver driver = new WebDriver();
* but be careful with other stuff :-)
*/
driver = new FirefoxDriver();
//Maximize window of FF
driver.manage().window().maximize();
}
//Annotation that sends to JUnit message that this method should be executed as TEST method
@Test
public void testJUnitWebDriver() {
//Open FF and go to https://google.com.ua. Also you can use this variant -> driver.navigate().to("https://google.com.ua");
driver.get("https://google.com");
//Send request string to the search form
driver.findElement(By.name("q")).sendKeys("Geeksforless");
//Dynamic wait for results
WebElement myDynamicElement = (new WebDriverWait(driver, 10))
.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[@class='st']")));
//Add results to list
ArrayList<String> resultsList = new ArrayList<String>();
int iResults = driver.findElements(By.xpath(".//*[@id='rso']/div/div")).size();
for (int i = 1; i < iResults+1; i++) {
resultsList.add(driver.findElement(By.xpath(".//*[@id='rso']/div/div["+i+"]/div/h3/a")).getAttribute("href"));
resultsList.add(driver.findElement(By.xpath("//*[@id='rso']/div/div["+i+"]/div/div/div/span")).getText());
}
//Print results
resultsList.forEach(System.out::println);
//Open link
driver.get(driver.findElement(By.xpath("//a[@href='http://geeksforless.com/']")).getAttribute("href"));
//Wait for site to appear
WebElement myDynamicElement2 = new WebDriverWait(driver, 10)
.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//a[@href='http://geeksforless.com']")));
}
//Annotation that sends to JUnit message that this method should be executed AFTER test method
@After
public void tearDown() throws IOException, InterruptedException {
//Close browser and finish work of driver
driver.quit();
}
}