0% found this document useful (0 votes)
9 views15 pages

List, Map, Set

The document contains multiple Java classes that demonstrate the use of Selenium WebDriver to automate the process of adding a book to the cart on eBay. Each class follows a similar structure, initializing a WebDriver, searching for a book, selecting it, and validating the quantity added to the cart. The classes also include handling of window switches and basic validation of the cart's contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views15 pages

List, Map, Set

The document contains multiple Java classes that demonstrate the use of Selenium WebDriver to automate the process of adding a book to the cart on eBay. Each class follows a similar structure, initializing a WebDriver, searching for a book, selecting it, and validating the quantity added to the cart. The classes also include handling of window switches and basic validation of the cart's contents.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 15

1) Ebay Add to Cart:

package testmaven;

import java.util.ArrayList;

import java.util.Set;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class EbayAddToCart {

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

// Initialize WebDriver

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

// Open a website

driver.get("https://www.ebay.com/");
Thread.sleep(1000);

//

WebElement search=driver.findElement(By.xpath(("//input[@id=\"gh-ac\"]")));

search.sendKeys("book");

Thread.sleep(2000);

search.sendKeys(Keys.RETURN);

WebElement
FirstBook=driver.findElement(By.xpath("//span[contains(text(),'ENGLISH OLD BOOK')]"));

FirstBook.click();

Thread.sleep(2000);

//Change window

Set<String> s = driver.getWindowHandles();

ArrayList al = new ArrayList (s);

System.out.println(al.get(0));

System.out.println(al.get(1));
driver.switchTo().window((String)al.get(1));

Thread.sleep(2000);

WebElement quantity=driver.findElement(By.xpath(("//*[@id=\"qtyTextBox\"]")));

quantity.clear();

quantity.sendKeys("2");

WebElement AddCart=driver.findElement(By.xpath("//span[text()='Add to
cart']"));

AddCart.click();

Thread.sleep(2000);

// Locate the span element using XPath

WebElement badgeElement = driver.findElement(By.xpath("//span[@class='badge'


and @role='img']"));

// Validate that the text inside the span is "1"

if (badgeElement.getText().equals("2")) {

System.out.println("Validation Passed: Added quantity of Book is 2");

} else {

System.out.println("Validation Failed: Expected '2', but found '" +


badgeElement.getText() + "'");

}
}

2)Test Array List:

package testmaven;

import java.util.ArrayList;

import java.util.Set;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class EbayAddToCart {

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

{
// Initialize WebDriver

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

// Open a website

driver.get("https://www.ebay.com/");

Thread.sleep(1000);

//

WebElement search=driver.findElement(By.xpath(("//input[@id=\"gh-ac\"]")));

search.sendKeys("book");

Thread.sleep(2000);

search.sendKeys(Keys.RETURN);

WebElement
FirstBook=driver.findElement(By.xpath("//span[contains(text(),'ENGLISH OLD BOOK')]"));

FirstBook.click();
Thread.sleep(2000);

//Change window

Set<String> s = driver.getWindowHandles();

ArrayList al = new ArrayList (s);

System.out.println(al.get(0));

System.out.println(al.get(1));

driver.switchTo().window((String)al.get(1));

Thread.sleep(2000);

WebElement quantity=driver.findElement(By.xpath(("//*[@id=\"qtyTextBox\"]")));

quantity.clear();

quantity.sendKeys("2");

WebElement AddCart=driver.findElement(By.xpath("//span[text()='Add to
cart']"));

AddCart.click();

Thread.sleep(2000);

// Locate the span element using XPath

WebElement badgeElement = driver.findElement(By.xpath("//span[@class='badge'


and @role='img']"));
// Validate that the text inside the span is "1"

if (badgeElement.getText().equals("2")) {

System.out.println("Validation Passed: Added quantity of Book is 2");

} else {

System.out.println("Validation Failed: Expected '2', but found '" +


badgeElement.getText() + "'");

3)Test Hashmap:

package testmaven;

import java.util.ArrayList;

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class EbayAddToCart {

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

// Initialize WebDriver

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();

// Open a website

driver.get("https://www.ebay.com/");

Thread.sleep(1000);

//

WebElement search=driver.findElement(By.xpath(("//input[@id=\"gh-ac\"]")));

search.sendKeys("book");
Thread.sleep(2000);

search.sendKeys(Keys.RETURN);

WebElement
FirstBook=driver.findElement(By.xpath("//span[contains(text(),'ENGLISH OLD BOOK')]"));

FirstBook.click();

Thread.sleep(2000);

//Change window

Set<String> s = driver.getWindowHandles();

ArrayList al = new ArrayList (s);

System.out.println(al.get(0));

System.out.println(al.get(1));

driver.switchTo().window((String)al.get(1));

Thread.sleep(2000);

WebElement quantity=driver.findElement(By.xpath(("//*[@id=\"qtyTextBox\"]")));

quantity.clear();

quantity.sendKeys("2");
WebElement AddCart=driver.findElement(By.xpath("//span[text()='Add to
cart']"));

AddCart.click();

Thread.sleep(2000);

// Locate the span element using XPath

WebElement badgeElement = driver.findElement(By.xpath("//span[@class='badge'


and @role='img']"));

// Validate that the text inside the span is "1"

if (badgeElement.getText().equals("2")) {

System.out.println("Validation Passed: Added quantity of Book is 2");

} else {

System.out.println("Validation Failed: Expected '2', but found '" +


badgeElement.getText() + "'");

}
4)Test Hashset:

package testmaven;

import java.util.ArrayList;

import java.util.Set;

import org.openqa.selenium.By;

import org.openqa.selenium.Keys;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.chrome.ChromeDriver;

public class EbayAddToCart {

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

// Initialize WebDriver

WebDriver driver = new ChromeDriver();

driver.manage().window().maximize();
// Open a website

driver.get("https://www.ebay.com/");

Thread.sleep(1000);

//

WebElement search=driver.findElement(By.xpath(("//input[@id=\"gh-ac\"]")));

search.sendKeys("book");

Thread.sleep(2000);

search.sendKeys(Keys.RETURN);

WebElement
FirstBook=driver.findElement(By.xpath("//span[contains(text(),'ENGLISH OLD BOOK')]"));

FirstBook.click();

Thread.sleep(2000);

//Change window

Set<String> s = driver.getWindowHandles();

ArrayList al = new ArrayList (s);

System.out.println(al.get(0));
System.out.println(al.get(1));

driver.switchTo().window((String)al.get(1));

Thread.sleep(2000);

WebElement quantity=driver.findElement(By.xpath(("//*[@id=\"qtyTextBox\"]")));

quantity.clear();

quantity.sendKeys("2");

WebElement AddCart=driver.findElement(By.xpath("//span[text()='Add to
cart']"));

AddCart.click();

Thread.sleep(2000);

// Locate the span element using XPath

WebElement badgeElement = driver.findElement(By.xpath("//span[@class='badge'


and @role='img']"));

// Validate that the text inside the span is "1"

if (badgeElement.getText().equals("2")) {

System.out.println("Validation Passed: Added quantity of Book is 2");

} else {

System.out.println("Validation Failed: Expected '2', but found '" +


badgeElement.getText() + "'");

}
}

5) Test Webdriver:

package testmaven;

import java.util.ArrayList;

import java.util.Set;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.chrome.ChromeDriver;

public class TestWebdriver {

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


WebDriver driver= new ChromeDriver();

driver.manage().window().maximize();

driver.get("https://www.youtube.com/");

Thread.sleep(2000);

Set<String> s=driver.getWindowHandles();

ArrayList al=new ArrayList(s);

System.out.println("Given Window is:"+ al);

driver.close();

You might also like