Skip to content

Commit d973e62

Browse files
committed
Advanced interactions with Actions class
1 parent 6a12d8c commit d973e62

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

webdriver_java/src/main/java/pages/HomePage.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ public DropdownPage clickDropDown(){
2121
return new DropdownPage(driver);
2222
}
2323

24+
public HoversPage clickHovers(){
25+
clickLink("Hovers");
26+
return new HoversPage(driver);
27+
}
28+
2429
private void clickLink(String linkText){
2530
driver.findElement(By.linkText(linkText)).click();
2631
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package pages;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebDriver;
5+
import org.openqa.selenium.WebElement;
6+
import org.openqa.selenium.interactions.Actions;
7+
8+
public class HoversPage {
9+
private WebDriver driver;
10+
11+
private By figureBox = By.className("figure");
12+
private By boxCaption = By.className("figcaption");
13+
14+
public HoversPage(WebDriver driver){
15+
this.driver = driver;
16+
}
17+
18+
/**
19+
* @param index starts at 1
20+
*/
21+
public FigureCaption hoverOverFigure(int index){
22+
WebElement figure = driver.findElements(figureBox).get(index - 1);
23+
24+
Actions actions = new Actions(driver);
25+
actions.moveToElement(figure).perform();
26+
27+
return new FigureCaption(figure.findElement(boxCaption));
28+
}
29+
30+
public class FigureCaption{
31+
32+
private WebElement caption;
33+
private By header = By.tagName("h5");
34+
private By link = By.tagName("a");
35+
36+
public FigureCaption(WebElement caption){
37+
this.caption = caption;
38+
}
39+
40+
public boolean isCaptionDisplayed(){
41+
return caption.isDisplayed();
42+
}
43+
44+
public String getTitle(){
45+
return caption.findElement(header).getText();
46+
}
47+
48+
public String getLink(){
49+
return caption.findElement(link).getAttribute("href");
50+
}
51+
52+
public String getLinkText(){
53+
return caption.findElement(link).getText();
54+
}
55+
}
56+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package hover;
2+
3+
import base.BaseTests;
4+
import org.testng.annotations.Test;
5+
import pages.HoversPage;
6+
7+
import static org.testng.Assert.*;
8+
9+
public class HoverTests extends BaseTests {
10+
11+
@Test
12+
public void testHoverUser1(){
13+
var hoversPage = homePage.clickHovers();
14+
var caption = hoversPage.hoverOverFigure(1);
15+
assertTrue(caption.isCaptionDisplayed(), "Caption not displayed");
16+
assertEquals(caption.getTitle(), "name: user1", "Caption title incorrect");
17+
assertEquals(caption.getLinkText(), "View profile", "Caption link text incorrect");
18+
assertTrue(caption.getLink().endsWith("/users/1"), "Link incorrect");
19+
}
20+
}

0 commit comments

Comments
 (0)