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 6af051e

Browse files
Added code on Java and TestNG
1 parent 7e976fd commit 6af051e

File tree

7 files changed

+368
-24
lines changed

7 files changed

+368
-24
lines changed

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
import java.nio.charset.StandardCharsets;
11+
import java.util.Random;
12+
13+
public class UtilsMethods {
14+
public void wait(RemoteWebDriver driver, By locator, int waitTime) {
15+
WebDriverWait wait = new WebDriverWait(driver, waitTime);
16+
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
17+
}
18+
19+
public void mouseHoverOnElement(RemoteWebDriver driver, By locator) {
20+
Actions actions = new Actions(driver);
21+
WebElement ele = driver.findElement(locator);
22+
actions.moveToElement(ele).build().perform();
23+
}
24+
25+
public String getRandomString(int size) {
26+
byte[] bytArray = new byte[256];
27+
new Random().nextBytes(bytArray);
28+
29+
String randomStr = new String(bytArray, StandardCharsets.UTF_8);
30+
StringBuilder strBuilder = new StringBuilder();
31+
// remove all special char
32+
String alphaStr = randomStr.replaceAll("[^A-Za-z]", "");
33+
34+
for (int i = 0; i < alphaStr.length(); i++) {
35+
if (size > 0 && (Character.isLetter(alphaStr.charAt(i)) || Character.isDigit(alphaStr.charAt(i)))) {
36+
strBuilder.append(alphaStr.charAt(i));
37+
}
38+
size--;
39+
}
40+
return strBuilder.toString();
41+
}
42+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.lambdatest;
2+
3+
import Utills.UtilsMethods;
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 AddProduct {
17+
private RemoteWebDriver driver;
18+
19+
private String Status = "failed";
20+
21+
@BeforeMethod
22+
public void setup(Method m, ITestContext ctx) throws MalformedURLException {
23+
// String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
24+
String username = System.getProperty("LT_USERNAME");
25+
// String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
26+
String authKey = System.getProperty("LT_ACCESS_KEY");
27+
String hub = "@hub.lambdatest.com/wd/hub";
28+
DesiredCapabilities caps = new DesiredCapabilities();
29+
caps.setCapability("platform", "win10");
30+
caps.setCapability("browserName", "chrome");
31+
caps.setCapability("version", "latest");
32+
caps.setCapability("build", System.getProperty("Build_Name") == null ? m.getName() + " - " + this.getClass().getName() : System.getProperty("Build_Name"));
33+
caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
34+
caps.setCapability("plugin", "git-testng");
35+
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
36+
caps.setCapability("tags", Tags);
37+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
38+
}
39+
40+
@Test
41+
public void addProducts() {
42+
driver.get("https://ecommerce-playground.lambdatest.io/");
43+
driver.findElement(By.cssSelector("shop-by-category")).click();
44+
driver.findElement(By.cssSelector(".mz-pure-drawer:first-of-type .navbar-nav>li:nth-of-type(3)")).click();
45+
driver.findElement(By.cssSelector("#container .manufacturer .mz-filter-group-content div:first-of-type div")).click();
46+
driver.findElement(By.cssSelector(".carousel-item:first-of-type [title='HTC Touch HD']"));
47+
driver.findElement(By.cssSelector("div[data-view_id='grid'] .product-layout:first-of-type button[title='Add to Cart']")).click();
48+
driver.findElement(By.cssSelector("#notification-box-top .btn-primary")).click();
49+
driver.findElement(By.cssSelector("#content .btn-secondary")).click();
50+
driver.findElement(By.xpath("//strong[contains(text(),'This is a dummy website for Web Automation Testing')]")).isSelected();
51+
Status = "Passed";
52+
}
53+
54+
@AfterMethod
55+
public void tearDown() {
56+
driver.executeScript("lambda-status=" + Status);
57+
driver.quit();
58+
}
59+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.lambdatest;
2+
3+
import Utills.UtilsMethods;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.Keys;
6+
import org.openqa.selenium.remote.DesiredCapabilities;
7+
import org.openqa.selenium.remote.RemoteWebDriver;
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 CompareProducts {
18+
private RemoteWebDriver driver;
19+
UtilsMethods methods = new UtilsMethods();
20+
21+
private String Status = "failed";
22+
23+
@BeforeMethod
24+
public void setup(Method m, ITestContext ctx) throws MalformedURLException {
25+
// String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
26+
String username = System.getProperty("LT_USERNAME");
27+
// String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
28+
String authKey = System.getProperty("LT_ACCESS_KEY");
29+
String hub = "@hub.lambdatest.com/wd/hub";
30+
DesiredCapabilities caps = new DesiredCapabilities();
31+
caps.setCapability("platform", "win10");
32+
caps.setCapability("browserName", "chrome");
33+
caps.setCapability("version", "latest");
34+
caps.setCapability("build", System.getProperty("Build_Name") == null ? m.getName() + " - " + this.getClass().getName() : System.getProperty("Build_Name"));
35+
caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
36+
caps.setCapability("plugin", "git-testng");
37+
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
38+
caps.setCapability("tags", Tags);
39+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
40+
}
41+
42+
@Test
43+
public void compareProducts() {
44+
driver.get("https://ecommerce-playground.lambdatest.io/");
45+
driver.findElement(By.cssSelector("shop-by-category")).click();
46+
driver.findElement(By.cssSelector(".mz-pure-drawer:first-of-type .navbar-nav>li:nth-of-type(3)")).click();
47+
driver.findElement(By.cssSelector(".carousel-item:first-of-type [title='HTC Touch HD']"));
48+
methods.mouseHoverOnElement(driver, By.cssSelector(".carousel-item:first-of-type [title='HTC Touch HD']"));
49+
driver.findElement(By.cssSelector("div[data-view_id='grid'] .product-layout:first-of-type button[title='Compare this Product']")).click();
50+
driver.findElement(By.cssSelector("#container .manufacturer .mz-filter-group-content div:first-of-type div")).click();
51+
methods.mouseHoverOnElement(driver, By.cssSelector(".carousel-item:first-of-type [title='iPod Touch']"));
52+
driver.findElement(By.cssSelector("div[data-view_id='grid'] .product-layout:first-of-type button[title='Compare this Product']")).click();
53+
driver.findElement(By.cssSelector("#notification-box-top a.btn-secondary")).click();
54+
methods.wait(driver, By.cssSelector(".h4"), 30);
55+
}
56+
57+
@AfterMethod
58+
public void tearDown() {
59+
driver.executeScript("lambda-status=" + Status);
60+
driver.quit();
61+
}
62+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.lambdatest;
2+
3+
import org.openqa.selenium.By;
4+
import org.openqa.selenium.Keys;
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 ProductFilters {
17+
private RemoteWebDriver driver;
18+
private String Status = "failed";
19+
20+
@BeforeMethod
21+
public void setup(Method m, ITestContext ctx) throws MalformedURLException {
22+
// String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
23+
String username = System.getProperty("LT_USERNAME");
24+
// String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
25+
String authKey = System.getProperty("LT_ACCESS_KEY");
26+
String hub = "@hub.lambdatest.com/wd/hub";
27+
DesiredCapabilities caps = new DesiredCapabilities();
28+
caps.setCapability("platform", "win10");
29+
caps.setCapability("browserName", "chrome");
30+
caps.setCapability("version", "latest");
31+
caps.setCapability("build", System.getProperty("Build_Name") == null ? m.getName() + " - " + this.getClass().getName() : System.getProperty("Build_Name"));
32+
caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
33+
caps.setCapability("plugin", "git-testng");
34+
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
35+
caps.setCapability("tags", Tags);
36+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
37+
}
38+
39+
@Test
40+
public void productFilters() {
41+
driver.get("https://ecommerce-playground.lambdatest.io/");
42+
driver.findElement(By.cssSelector("#container input[name='mz_fp[min]']")).clear();
43+
driver.findElement(By.cssSelector("#container input[name='mz_fp[min]']")).sendKeys("0");
44+
driver.findElement(By.cssSelector("#container input[name='mz_fp[max]']")).clear();
45+
driver.findElement(By.cssSelector("#container input[name='mz_fp[max]']")).sendKeys("200");
46+
driver.findElement(By.cssSelector("#container input[name='mz_fp[max]']")).sendKeys(Keys.ENTER);
47+
driver.findElement(By.cssSelector("#container .manufacturer .mz-filter-group-content div:first-of-type div")).click();
48+
driver.findElement(By.cssSelector("#container .module-category a:nth-of-type(5)")).click();
49+
}
50+
51+
@AfterMethod
52+
public void tearDown() {
53+
driver.executeScript("lambda-status=" + Status);
54+
driver.quit();
55+
}
56+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.lambdatest;
2+
3+
import Utills.UtilsMethods;
4+
import org.openqa.selenium.By;
5+
import org.openqa.selenium.Keys;
6+
import org.openqa.selenium.remote.DesiredCapabilities;
7+
import org.openqa.selenium.remote.RemoteWebDriver;
8+
import org.openqa.selenium.support.ui.Select;
9+
import org.testng.Assert;
10+
import org.testng.ITestContext;
11+
import org.testng.annotations.AfterMethod;
12+
import org.testng.annotations.BeforeMethod;
13+
import org.testng.annotations.Test;
14+
15+
import java.lang.reflect.Method;
16+
import java.net.MalformedURLException;
17+
import java.net.URL;
18+
19+
public class ProductPage {
20+
private RemoteWebDriver driver;
21+
private String Status = "failed";
22+
23+
@BeforeMethod
24+
public void setup(Method m, ITestContext ctx) throws MalformedURLException {
25+
// String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
26+
String username = System.getProperty("LT_USERNAME");
27+
// String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
28+
String authKey = System.getProperty("LT_ACCESS_KEY");
29+
String hub = "@hub.lambdatest.com/wd/hub";
30+
DesiredCapabilities caps = new DesiredCapabilities();
31+
caps.setCapability("platform", "win10");
32+
caps.setCapability("browserName", "chrome");
33+
caps.setCapability("version", "latest");
34+
caps.setCapability("build", System.getProperty("Build_Name") == null ? m.getName() + " - " + this.getClass().getName() : System.getProperty("Build_Name"));
35+
caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
36+
caps.setCapability("plugin", "git-testng");
37+
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
38+
caps.setCapability("tags", Tags);
39+
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
40+
}
41+
42+
@Test
43+
public void searchProduct() {
44+
driver.get("https://ecommerce-playground.lambdatest.io/");
45+
driver.findElement(By.cssSelector("shop-by-category")).click();
46+
driver.findElement(By.cssSelector(".mz-pure-drawer:first-of-type .navbar-nav>li:nth-of-type(3)")).click();
47+
driver.findElement(By.cssSelector("#container .manufacturer .mz-filter-group-content div:first-of-type div")).click();
48+
driver.findElement(By.cssSelector(".order-1 .btn-cart")).click();
49+
}
50+
51+
@AfterMethod
52+
public void tearDown() {
53+
driver.executeScript("lambda-status=" + Status);
54+
driver.quit();
55+
}
56+
}

‎src/test/java/com/lambdatest/SampleAutomation.java renamed to ‎src/test/java/com/lambdatest/RegisterAccount.java

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

3+
import Utills.UtilsMethods;
34
import org.openqa.selenium.By;
5+
import org.openqa.selenium.Keys;
46
import org.openqa.selenium.remote.DesiredCapabilities;
57
import org.openqa.selenium.remote.RemoteWebDriver;
6-
import org.openqa.selenium.support.ui.ExpectedConditions;
7-
import org.openqa.selenium.support.ui.WebDriverWait;
8+
import org.openqa.selenium.support.ui.Select;
89
import org.testng.Assert;
910
import org.testng.ITestContext;
1011
import org.testng.annotations.AfterMethod;
@@ -15,63 +16,47 @@
1516
import java.net.MalformedURLException;
1617
import java.net.URL;
1718

18-
public class SampleAutomation {
19-
19+
public class RegisterAccount {
2020
private RemoteWebDriver driver;
21+
UtilsMethods methods = new UtilsMethods();
2122
private String Status = "failed";
2223

2324
@BeforeMethod
2425
public void setup(Method m, ITestContext ctx) throws MalformedURLException {
25-
// String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
26+
// String username = System.getenv("LT_USERNAME") == null ? "Your LT Username" : System.getenv("LT_USERNAME");
2627
String username = System.getProperty("LT_USERNAME");
27-
// String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
28+
// String authKey = System.getenv("LT_ACCESS_KEY") == null ? "Your LT AccessKey" : System.getenv("LT_ACCESS_KEY");
2829
String authKey = System.getProperty("LT_ACCESS_KEY");
29-
3030
String hub = "@hub.lambdatest.com/wd/hub";
31-
3231
DesiredCapabilities caps = new DesiredCapabilities();
3332
caps.setCapability("platform", "win10");
3433
caps.setCapability("browserName", "chrome");
3534
caps.setCapability("version", "latest");
3635
caps.setCapability("build", System.getProperty("Build_Name") == null ? m.getName() + " - " + this.getClass().getName() : System.getProperty("Build_Name"));
3736
caps.setCapability("name", m.getName() + " - " + this.getClass().getName());
3837
caps.setCapability("plugin", "git-testng");
39-
4038
String[] Tags = new String[] { "Feature", "Falcon", "Severe" };
4139
caps.setCapability("tags", Tags);
42-
4340
driver = new RemoteWebDriver(new URL("https://" + username + ":" + authKey + hub), caps);
44-
45-
}
46-
public void wait(By locator, int waitTime) {
47-
WebDriverWait wait = new WebDriverWait(driver, waitTime);
48-
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
4941
}
5042

5143
@Test(priority = 1)
5244
public void register() {
5345
driver.get("https://ecommerce-playground.lambdatest.io/");
5446
driver.findElement(By.cssSelector("#main-navigation a[href*='account/account']")).click();
5547
driver.findElement(By.cssSelector("#column-right a[href*='account/register']")).click();
56-
wait(By.id("input-firstname"), 30);
48+
methods.wait(driver, By.id("input-firstname"), 30);
5749
driver.findElement(By.id("input-firstname")).sendKeys("name");
5850
driver.findElement(By.id("input-lastname")).sendKeys("LastName");
5951
driver.findElement(By.id("input-email")).sendKeys("Email");
6052
driver.findElement(By.id("input-telephone")).sendKeys("Number");
6153
driver.findElement(By.id("input-password")).sendKeys("Password");
6254
driver.findElement(By.id("input-confirm")).sendKeys("Confirm password");
63-
// driver.findElement(By.id("input-agree")).click();
55+
// driver.findElement(By.id("input-agree")).click();
6456
driver.findElement(By.cssSelector("input[type='submit']")).click();
6557
System.out.println("TestFinished");
6658
}
6759

68-
// @Test(priority = 2)
69-
public void searchProduct() {
70-
driver.findElement(By.cssSelector("#main-header [placeholder='Search For Products']")).sendKeys("i pod Nano");
71-
driver.findElement(By.cssSelector("#search .dropdown ul>li:first-of-type")).click();
72-
driver.findElement(By.cssSelector(".order-1 .btn-cart")).click();
73-
}
74-
7560
@AfterMethod
7661
public void tearDown() {
7762
driver.executeScript("lambda-status=" + Status);

0 commit comments

Comments
(0)

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