selentest.java

1 0 0
                                    

//The client is looking for a test automation solution. We will help the client to design and implement the solution.
//Java, Selenium,

//package seleniumTestScripts;

import java.util.List;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class AwSelenTest1{
public static void main(String[]args)throws InterruptedException{

Scanner scan=new Scanner(System.in);
//String get1;String get2;String get3;String get4;String get5;String get6;String get7;

//1) Prompt the user for website to be accessed.

System.out.println("Enter the full name of website with https");
String get1=scan.nextLine();

// Now get the computer to go to the site.

System.setProperty("webdriver.chrome.driver","./drivers/chromedriver.exe");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.get(get1);

//2) Prompt the user for where to click on the page.

System.out.println("Where to mouse over to?");
String get2=scan.nextLine();
 
// Now the computer mouse over to click prompt. (Actions -> moveToElement)

Actions action = new Actions(driver);
WebElement element= driver.findElementByXPath("(//a[text()=get2])[1]");
action.moveToElement(element).perform();

//3) Another way to get the computer to click somewhere specific on a page.

System.out.println("Click what in this page?");String get3=scan.nextLine();

driver.findElementByXPath("//a[text()=get3]").click();

//4) Finding the total count of an item ((top) -> getText -> String)

String countAmount = driver.findElementByXPath("//span[@class='title-count']").getText();
//Before Remove
System.out.println(countAmount);
String countText = countAmount.replaceAll("\\D", "");
int countNum = Integer.parseInt(countText);
//After Remove
System.out.println(countNum);

// 5) Validate the sum of categories count matches

System.out.println("Count what on this page?");String get4=scan.nextLine();

System.out.println("Count what on this page else?");String get5=scan.nextLine();

driver.findElementByXPath("//label[text()=get4]").click();
String count1 = driver.findElementByXPath("(//span[@class='categories-num'])[1]").getText();
int sum1 = Integer.parseInt(count1.replaceAll("\\D", ""));
System.out.println("Total item1 Count " + sum1);

String count2 = driver.findElementByXPath("(//span[@class='categories-num'])[2]").getText();
int sum2= Integer.parseInt(count2.replaceAll("\\D", ""));
System.out.println("Total item1 Count " + sum2);

int totCount = sum1+sum2;
System.out.println("Sum of categories :" +totCount);
if(countNum==totCount) {
System.out.println("Sum of categories counts are matched");}
else {System.out.println("Sum of categories counts are not matched");}

// 6)  The second item.
driver.findElementByXPath("//label[text()=get4]").click();
driver.findElementByXPath("//label[text()=get5]").click();

// 7) Selecting an option on a webpage
System.out.println("Type in an option shown on the web page.");
String get6=scan.nextLine();

driver.findElementByXPath("//div[@class=get6]").click();

// 8) Type another option and click checkbox.
System.out.println("Type in an option on the new webpage.");
String get7=scan.nextLine();

driver.findElementByXPath("//input[@placeholder='Search brand']").sendKeys(get7);
driver.findElementByXPath("//label[@class=' common-customCheckbox']").click();

// 9) Close the pop-up x
driver.findElementByXPath("//span[@class='myntraweb-sprite FilterDirectory-close sprites-remove']").click();

Thread.sleep(5000);
// 10) Confirm all the Coats are of brand item
String brand = get7;
List<WebElement> eleBrand = driver.findElementsByXPath("//h3[@class='product-brand']");
for (WebElement eachBrand : eleBrand) {
String brandName = eachBrand.getText();
//System.out.println(brandName);
if(!brandName.equals(brand)) {
System.out.print("Non selected brand of Products listed");}}

// 11) Sort by Better Discount
WebElement eleSort = driver.findElementByXPath("//span[text()='Recommended']");
action.moveToElement(eleSort).perform();
driver.findElementByXPath("//label[text()='Better Discount']").click();

// 12) Find the price of first displayed item
List<WebElement> elePriceList = driver.findElementsByXPath("//span[@class='product-discountedPrice']");
String firstPrice = elePriceList.get(0).getText();
int price = Integer.parseInt(firstPrice.replaceAll("\\D", ""));
System.out.println("First discount price : " + price);

// 13) Mouse over on size of the first item
WebElement eleItem = driver.findElementByXPath("(//span[@class='product-discountedPrice'])[1]");
action.moveToElement(eleItem).perform();

// 14) Click on WishList Now
driver.findElementByXPath("((//span[@class='product-actionsButton product-addToBag']))[1]").click();

driver.findElementByXPath("(//button[@class='product-sizeButton'])[1]").click();

// 15) Now close The Browser
driver.close();}}

//Written by me, inspiration by Sudhar Sun

My Prog AdventuresWhere stories live. Discover now