Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 379d22c

Browse files
Added webdriver helper
1 parent 73a6f02 commit 379d22c

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package Utills;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.WebElement;
5+
import org.openqa.selenium.interactions.Actions;
6+
import org.openqa.selenium.remote.RemoteWebDriver;
7+
import org.openqa.selenium.support.ui.ExpectedConditions;
8+
import org.openqa.selenium.support.ui.WebDriverWait;
9+
10+
public class WebDriverHelper {
11+
private RemoteWebDriver driver;
12+
public WebDriverHelper(RemoteWebDriver driver) {
13+
super();
14+
this.driver = driver;
15+
}
16+
17+
18+
public void getURL(String url) {
19+
driver.get(url);
20+
}
21+
22+
public void click(By locator) {
23+
driver.findElement(locator).click();
24+
}
25+
26+
public boolean isDisplayed(By locator) {
27+
return driver.findElement(locator).isDisplayed();
28+
}
29+
public void waitForPresence(By locator, int waitTime) {
30+
WebDriverWait wait = new WebDriverWait(driver, waitTime);
31+
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
32+
}
33+
34+
public void waitForVisibility(By locator, int waitTime) {
35+
WebDriverWait wait = new WebDriverWait(driver, waitTime);
36+
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
37+
}
38+
39+
public void waitForClickable(By locator, int waitTime) {
40+
WebDriverWait wait = new WebDriverWait(driver, waitTime);
41+
wait.until(ExpectedConditions.elementToBeClickable(locator));
42+
}
43+
44+
public void mouseHoverOnElement(By locator) {
45+
Actions actions = new Actions(driver);
46+
WebElement ele = driver.findElement(locator);
47+
actions.moveToElement(ele).build().perform();
48+
}
49+
50+
public void staleElementRefresh(By locator) {
51+
WebDriverWait wait = new WebDriverWait(driver, 30);
52+
wait.until(ExpectedConditions.stalenessOf(driver.findElement(locator)));
53+
}
54+
55+
public void waitForTime(int timeout) {
56+
try {
57+
Thread.sleep(timeout);
58+
} catch (Exception exception) {
59+
exception.printStackTrace();
60+
}
61+
}
62+
63+
64+
}

‎src/test/java/com/lambdatest/AddProduct.java

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.lambdatest;
22

33
import Utills.UtilsMethods;
4+
import Utills.WebDriverHelper;
45
import org.openqa.selenium.By;
56
import org.openqa.selenium.remote.DesiredCapabilities;
67
import org.openqa.selenium.remote.RemoteWebDriver;
8+
import org.testng.Assert;
79
import org.testng.ITestContext;
810
import org.testng.annotations.AfterMethod;
911
import org.testng.annotations.BeforeMethod;
@@ -15,7 +17,7 @@
1517

1618
public class AddProduct {
1719
private RemoteWebDriver driver;
18-
UtilsMethodsmethods = newUtilsMethods();
20+
WebDriverHelperdriverHelper;
1921

2022
private String Status = "failed";
2123

@@ -33,23 +35,27 @@ public class AddProduct {
3335
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
3436
caps.setCapability("tags", Tags);
3537
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
38+
driverHelper = new WebDriverHelper(driver);
3639
}
3740

3841
@Test public void addProducts() {
39-
driver.get("https://ecommerce-playground.lambdatest.io/");
40-
methods.wait(driver, By.className("shop-by-category"), 30);
41-
driver.findElement(By.className("shop-by-category")).click();
42-
driver.findElement(By.cssSelector(".mz-pure-drawer:first-of-type .navbar-nav>li:nth-of-type(3)")).click();
43-
driver.findElement(By.cssSelector("#container .manufacturer .mz-filter-group-content div:first-of-type div"))
44-
.click();
45-
driver.findElement(By.cssSelector(".carousel-item:first-of-type [title='HTC Touch HD']"));
46-
driver.findElement(
47-
By.cssSelector("div[data-view_id='grid'] .product-layout:first-of-type button[title='Add to Cart']"))
48-
.click();
49-
driver.findElement(By.cssSelector("#notification-box-top .btn-primary")).click();
50-
driver.findElement(By.cssSelector("#content .btn-secondary")).click();
51-
driver.findElement(By.xpath("//strong[contains(text(),'This is a dummy website for Web Automation Testing')]"))
52-
.isSelected();
42+
driverHelper.getURL("https://ecommerce-playground.lambdatest.io/");
43+
driverHelper.waitForPresence(By.className("shop-by-category"), 30);
44+
driverHelper.click(By.className("shop-by-category"));
45+
driverHelper.click(By.cssSelector(".mz-pure-drawer:first-of-type .navbar-nav>li:nth-of-type(3)"));
46+
driverHelper.click(By.cssSelector("#container .manufacturer .mz-filter-group-content div:first-of-type div"));
47+
driverHelper.mouseHoverOnElement(By.cssSelector(".carousel-item:first-of-type [title='iPod Touch']"));
48+
driverHelper.staleElementRefresh(By.cssSelector(".carousel-item:first-of-type [title='iPod Touch']"));
49+
driverHelper.waitForTime(5);
50+
driverHelper.mouseHoverOnElement(By.cssSelector(".carousel-item:first-of-type [title='iPod Touch']"));
51+
driverHelper.waitForClickable(By.cssSelector("div[data-view_id='grid'] .product-layout:first-of-type button[title='Add to Cart']"), 30);
52+
driverHelper.click(By.cssSelector("div[data-view_id='grid'] .product-layout:first-of-type button[title='Add to Cart']"));
53+
driverHelper.click(By.cssSelector("#notification-box-top .btn-primary"));
54+
driverHelper.waitForVisibility(By.cssSelector("#content .btn-secondary"), 30);
55+
driverHelper.click(By.cssSelector("#content .btn-secondary"));
56+
driverHelper.waitForVisibility(By.xpath("//strong[contains(text(),'This is a dummy website for Web Automation Testing')]"), 30);
57+
boolean value = driver.findElement(By.xpath("//strong[contains(text(),'This is a dummy website for Web Automation Testing')]")).isDisplayed();
58+
Assert.assertTrue(value, "Element is not displayed.");
5359
Status = "Passed";
5460
}
5561

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /