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 d76f39f

Browse files
Merge pull request #10 from Saurabh-LT/MLE-9930-java-test-ng-onboarding-automation
register automation done
2 parents a6e9666 + a483d96 commit d76f39f

15 files changed

+655
-10
lines changed

‎single.xml renamed to ‎AddproductSingle.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<suite thread-count="1" parallel="tests" name="Suite">
44
<test thread-count="1" parallel="classes" name="Test">
55
<classes>
6-
<class name="com.lambdatest.TestNGTodo2"/>
6+
<class name="com.lambdatest.AddProduct"/>
77
</classes>
88
</test> <!-- Test -->
99
</suite> <!-- Suite -->

‎CompareProductsSingle.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite thread-count="1" parallel="tests" name="Suite">
4+
<test thread-count="1" parallel="classes" name="Test">
5+
<classes>
6+
<class name="com.lambdatest.CompareProducts"/>
7+
</classes>
8+
</test> <!-- Test -->
9+
</suite> <!-- Suite -->

‎ProductFilterSingle.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite thread-count="1" parallel="tests" name="Suite">
4+
<test thread-count="1" parallel="classes" name="Test">
5+
<classes>
6+
<class name="com.lambdatest.ProductFilters"/>
7+
</classes>
8+
</test> <!-- Test -->
9+
</suite> <!-- Suite -->

‎ProductPageSingle.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite thread-count="1" parallel="tests" name="Suite">
4+
<test thread-count="1" parallel="classes" name="Test">
5+
<classes>
6+
<class name="com.lambdatest.ProductPage"/>
7+
</classes>
8+
</test> <!-- Test -->
9+
</suite> <!-- Suite -->

‎PurchaseProductsSingle.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite thread-count="1" parallel="tests" name="Suite">
4+
<test thread-count="1" parallel="classes" name="Test">
5+
<classes>
6+
<class name="com.lambdatest.purchaseProduct"/>
7+
</classes>
8+
</test> <!-- Test -->
9+
</suite> <!-- Suite -->

‎RegisterAccountSingle.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
3+
<suite thread-count="1" parallel="tests" name="Suite">
4+
<test thread-count="1" parallel="classes" name="Test">
5+
<classes>
6+
<class name="com.lambdatest.RegisterAccount"/>
7+
</classes>
8+
</test> <!-- Test -->
9+
</suite> <!-- Suite -->

‎src/test/java/Utills/UtilsMethods.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package Utills;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Random;
5+
6+
public class UtilsMethods {
7+
8+
public String getRandomString(int size) {
9+
byte[] bytArray = new byte[256];
10+
new Random().nextBytes(bytArray);
11+
12+
String randomStr = new String(bytArray, StandardCharsets.UTF_8);
13+
StringBuilder strBuilder = new StringBuilder();
14+
// remove all special char
15+
String alphaStr = randomStr.replaceAll("[^A-Za-z]", "");
16+
17+
for (int i = 0; i < alphaStr.length(); i++) {
18+
if (size > 0 && (Character.isLetter(alphaStr.charAt(i)) || Character.isDigit(alphaStr.charAt(i)))) {
19+
strBuilder.append(alphaStr.charAt(i));
20+
}
21+
size--;
22+
}
23+
return strBuilder.toString();
24+
}
25+
26+
public String generateRandomNumber(int length) {
27+
StringBuilder builder = new StringBuilder();
28+
Random objGenerator = new Random();
29+
while (length > 0) {
30+
int randomNumber = objGenerator.nextInt(9);
31+
builder.append(randomNumber);
32+
length--;
33+
}
34+
return builder.toString();
35+
}
36+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package Utills;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.Keys;
5+
import org.openqa.selenium.StaleElementReferenceException;
6+
import org.openqa.selenium.WebElement;
7+
import org.openqa.selenium.interactions.Actions;
8+
import org.openqa.selenium.remote.RemoteWebDriver;
9+
import org.openqa.selenium.support.ui.ExpectedConditions;
10+
import org.openqa.selenium.support.ui.Select;
11+
import org.openqa.selenium.support.ui.WebDriverWait;
12+
13+
public class WebDriverHelper {
14+
private RemoteWebDriver driver;
15+
16+
public WebDriverHelper(RemoteWebDriver driver) {
17+
super();
18+
this.driver = driver;
19+
}
20+
21+
public void getURL(String url) {
22+
driver.get(url);
23+
}
24+
25+
public void click(By locator) {
26+
driver.findElement(locator).click();
27+
}
28+
29+
public void submit(By locator) {
30+
driver.findElement(locator).submit();
31+
}
32+
33+
public void sendKeys(By locator, String str) {
34+
driver.findElement(locator).sendKeys(str);
35+
}
36+
37+
public void clearInputField(By locator) {
38+
driver.findElement(locator).clear();
39+
}
40+
41+
public void sendKeysByKeyBoard(By locator, Keys keys) {
42+
driver.findElement(locator).sendKeys(keys);
43+
}
44+
45+
46+
public String getText(By locator) {
47+
return driver.findElement(locator).getText();
48+
}
49+
50+
public void selectDropDownByValue(By locator, String value) {
51+
Select select = new Select(driver.findElement(locator));
52+
select.selectByValue(value);
53+
}
54+
55+
public boolean isDisplayed(By locator) {
56+
return driver.findElement(locator).isDisplayed();
57+
}
58+
59+
public void waitForPresence(By locator, int waitTime) {
60+
WebDriverWait wait = new WebDriverWait(driver, waitTime);
61+
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
62+
}
63+
64+
public void waitForVisibility(By locator, int waitTime) {
65+
WebDriverWait wait = new WebDriverWait(driver, waitTime);
66+
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
67+
}
68+
69+
public void waitForClickable(By locator, int waitTime) {
70+
WebDriverWait wait = new WebDriverWait(driver, waitTime);
71+
wait.until(ExpectedConditions.elementToBeClickable(locator));
72+
}
73+
74+
public void mouseHoverOnElement(By locator) {
75+
Actions actions = new Actions(driver);
76+
WebElement ele = driver.findElement(locator);
77+
actions.moveToElement(ele).build().perform();
78+
}
79+
80+
public void staleElementRefresh(By locator) {
81+
try {
82+
WebDriverWait wait = new WebDriverWait(driver, 30);
83+
wait.until(ExpectedConditions.stalenessOf(driver.findElement(locator)));
84+
} catch (StaleElementReferenceException exception) {
85+
exception.printStackTrace();
86+
}
87+
}
88+
89+
public void waitForTime(int timeout) {
90+
try {
91+
Thread.sleep(timeout);
92+
} catch (Exception exception) {
93+
exception.printStackTrace();
94+
}
95+
}
96+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package com.lambdatest;
2+
3+
import Utills.WebDriverHelper;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.remote.DesiredCapabilities;
6+
import org.openqa.selenium.remote.RemoteWebDriver;
7+
import org.testng.Assert;
8+
import org.testng.ITestContext;
9+
import org.testng.annotations.AfterMethod;
10+
import org.testng.annotations.BeforeMethod;
11+
import org.testng.annotations.Test;
12+
13+
import java.lang.reflect.Method;
14+
import java.net.MalformedURLException;
15+
import java.net.URL;
16+
17+
public class AddProduct {
18+
private RemoteWebDriver driver;
19+
WebDriverHelper driverHelper;
20+
21+
22+
//Elements
23+
protected static final By SHOP_BY_CATEGORY_NAVIGATION = By.className("shop-by-category");
24+
protected static final By PHONE_TABLETS_IPOD_NAVIGATION = By.cssSelector(".mz-pure-drawer:first-of-type .navbar-nav>li:nth-of-type(3)");
25+
protected static final By APPLE_MANUFACTURER_FILTER = By.cssSelector("#container .manufacturer .mz-filter-group-content div:first-of-type div");
26+
protected static final By FIRST_IPOD_PRODUCT = By.cssSelector(".carousel-item:first-of-type [title='iPod Touch']");
27+
protected static final By ADD_TO_CART_FIRST_PRODUCT = By.cssSelector("div[data-view_id='grid'] .product-layout:first-of-type button[title='Add to Cart']");
28+
protected static final By VIEW_CART_BUTTON_IN_BOX = By.cssSelector("#notification-box-top .btn-primary");
29+
protected static final By CONTINUE_SHOPPING_BUTTON = By.cssSelector("#content .btn-secondary");
30+
protected static final By WEBSITE_DISCLAIMER_HEADING_ON_HOME_PAGE = By.xpath("//strong[contains(text(),'This is a dummy website for Web Automation Testing')]");
31+
32+
private String Status = "failed";
33+
34+
@BeforeMethod public void setup(Method m, ITestContext ctx) throws MalformedURLException {
35+
String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
36+
String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
37+
String hub = "@hub.lambdatest.com/wd/hub";
38+
DesiredCapabilities caps = new DesiredCapabilities();
39+
caps.setCapability("platform", "win10");
40+
caps.setCapability("browserName", "chrome");
41+
caps.setCapability("version", "latest");
42+
caps.setCapability("build", "TestNG With Java");
43+
caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
44+
caps.setCapability("plugin", "git-testng");
45+
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
46+
caps.setCapability("tags", Tags);
47+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
48+
driverHelper = new WebDriverHelper(driver);
49+
}
50+
51+
@Test public void addProducts() {
52+
driverHelper.getURL("https://ecommerce-playground.lambdatest.io/");
53+
driverHelper.waitForPresence(SHOP_BY_CATEGORY_NAVIGATION, 30);
54+
driverHelper.click(SHOP_BY_CATEGORY_NAVIGATION);
55+
driverHelper.click(PHONE_TABLETS_IPOD_NAVIGATION);
56+
driverHelper.click(APPLE_MANUFACTURER_FILTER);
57+
driverHelper.mouseHoverOnElement(FIRST_IPOD_PRODUCT);
58+
driverHelper.staleElementRefresh(FIRST_IPOD_PRODUCT);
59+
driverHelper.waitForTime(5);
60+
driverHelper.mouseHoverOnElement(FIRST_IPOD_PRODUCT);
61+
driverHelper.waitForClickable(ADD_TO_CART_FIRST_PRODUCT, 30);
62+
driverHelper.click(ADD_TO_CART_FIRST_PRODUCT);
63+
driverHelper.click(VIEW_CART_BUTTON_IN_BOX);
64+
driverHelper.waitForVisibility(CONTINUE_SHOPPING_BUTTON, 30);
65+
driverHelper.click(CONTINUE_SHOPPING_BUTTON);
66+
driverHelper.waitForVisibility(WEBSITE_DISCLAIMER_HEADING_ON_HOME_PAGE, 30);
67+
boolean value = driver.findElement(WEBSITE_DISCLAIMER_HEADING_ON_HOME_PAGE).isDisplayed();
68+
Assert.assertTrue(value, "Element is not displayed.");
69+
Status = "passed";
70+
}
71+
72+
@AfterMethod public void tearDown() {
73+
driver.executeScript("lambda-status=" + Status);
74+
driver.quit();
75+
}
76+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.lambdatest;
2+
3+
import Utills.WebDriverHelper;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.remote.DesiredCapabilities;
6+
import org.openqa.selenium.remote.RemoteWebDriver;
7+
import org.testng.ITestContext;
8+
import org.testng.annotations.AfterMethod;
9+
import org.testng.annotations.BeforeMethod;
10+
import org.testng.annotations.Test;
11+
12+
import java.lang.reflect.Method;
13+
import java.net.MalformedURLException;
14+
import java.net.URL;
15+
16+
public class CompareProducts {
17+
private RemoteWebDriver driver;
18+
WebDriverHelper driverHelper;
19+
20+
private String Status = "failed";
21+
22+
//Elements
23+
protected static final By SHOP_BY_CATEGORY_NAVIGATION = By.className("shop-by-category");
24+
protected static final By PHONE_TABLETS_IPOD_NAVIGATION = By.cssSelector(
25+
".mz-pure-drawer:first-of-type .navbar-nav>li:nth-of-type(3)");
26+
protected static final By HTC_TOUCH_ID_FIRST_PRODUCT = By.cssSelector(
27+
".carousel-item:first-of-type [title='HTC Touch HD']");
28+
protected static final By COMPARE_TO_THIS_PRODUCT_OPTION = By.cssSelector(
29+
"div[data-view_id='grid'] .product-layout:first-of-type button[title='Compare this Product']");
30+
protected static final By APPLE_MANUFACTURER_FILTER = By.cssSelector(
31+
"#container .manufacturer .mz-filter-group-content div:first-of-type div");
32+
protected static final By FIRST_IPOD_PRODUCT = By.cssSelector(".carousel-item:first-of-type [title='iPod Touch']");
33+
protected static final By COMPARE_PRODUCT_BUTTON = By.cssSelector("#notification-box-top a.btn-secondary");
34+
protected static final By PRODUCT_COMPARISON_HEADING = By.cssSelector(".h4");
35+
36+
@BeforeMethod public void setup(Method m, ITestContext ctx) throws MalformedURLException {
37+
String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
38+
String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
39+
String hub = "@hub.lambdatest.com/wd/hub";
40+
DesiredCapabilities caps = new DesiredCapabilities();
41+
caps.setCapability("platform", "win10");
42+
caps.setCapability("browserName", "chrome");
43+
caps.setCapability("version", "latest");
44+
caps.setCapability("build", "TestNG With Java");
45+
caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
46+
caps.setCapability("plugin", "git-testng");
47+
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
48+
caps.setCapability("tags", Tags);
49+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
50+
driverHelper = new WebDriverHelper(driver);
51+
}
52+
53+
@Test public void compareProducts() {
54+
driverHelper.getURL("https://ecommerce-playground.lambdatest.io/");
55+
driverHelper.click(SHOP_BY_CATEGORY_NAVIGATION);
56+
driverHelper.click(PHONE_TABLETS_IPOD_NAVIGATION);
57+
driverHelper.mouseHoverOnElement(HTC_TOUCH_ID_FIRST_PRODUCT);
58+
driverHelper.click(COMPARE_TO_THIS_PRODUCT_OPTION);
59+
driverHelper.click(APPLE_MANUFACTURER_FILTER);
60+
driverHelper.waitForVisibility(FIRST_IPOD_PRODUCT, 30);
61+
driverHelper.staleElementRefresh(FIRST_IPOD_PRODUCT);
62+
driverHelper.mouseHoverOnElement(FIRST_IPOD_PRODUCT);
63+
driverHelper.click(COMPARE_TO_THIS_PRODUCT_OPTION);
64+
driverHelper.click(COMPARE_PRODUCT_BUTTON);
65+
driverHelper.isDisplayed(PRODUCT_COMPARISON_HEADING);
66+
Status = "passed";
67+
}
68+
69+
@AfterMethod public void tearDown() {
70+
driver.executeScript("lambda-status=" + Status);
71+
driver.quit();
72+
}
73+
}

0 commit comments

Comments
(0)

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