selenium-program-1


// Importing necessary libraries
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.interactions.Actions;
public class Commands {
public static void main(String[] args) {
// Setup Chrome Driver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Pavani\\Desktop\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Open URL
String url = "https://www.amazon.in/";
driver.get(url);
// Fetch and display title
String title = driver.getTitle();
// Enter text in search box
WebElement elementToSendKeys = driver.findElement(By.xpath("//*[@id=\"twotabsearchtextbox\"]"));
elementToSendKeys.sendKeys("Electronics");
// Select category from dropdown
WebElement selectElement = driver.findElement(By.xpath("//*[@id=\"searchDropdownBox\"]"));
Select select = new Select(selectElement);
select.selectByVisibleText("Electronics");
// Click on search button
WebElement elementToClick = driver.findElement(By.xpath("//*[@id=\"nav-search-submit-button\"]"));
// Hover on menu item
WebElement menuItem = driver.findElement(By.xpath("//*[@id=\"nav-link-accountList\"]/span"));
Actions actions = new Actions(driver);
actions.moveToElement(menuItem).perform();
// Right click on an element
WebElement element = driver.findElement(By.xpath("//*[@id=\"nav-xshop\"]/a[1]"));
actions.contextClick(element).build().perform();
// Double click on an element
WebElement elementDoubleClick = driver.findElement(By.xpath("//*[@id=\"heroSection-container\"]/div[3]/div[2]/div[2]"));
actions.doubleClick(elementDoubleClick).build().perform();
// Click on checkboxes
driver.findElement(By.linkText("Sell")).click();
driver.findElement(By.linkText("Start here")).click();
// Print all image sources
List<WebElement> images = driver.findElements(By.tagName("img"));
for (WebElement image : images) {
String src = image.getAttribute("src");
System.out.println(src);
}
// Check if image is displayed
WebElement image = driver.findElement(By.xpath("//*[@id=\"nav-logo-sprites\"]"));
boolean isDisplayed = image.isDisplayed();
if (isDisplayed) {
System.out.println("The image is displayed.");
} else {
System.out.println("The image is not displayed.");
}
// Get image size
Dimension size = image.getSize();
System.out.println("Image size: " + size.getWidth() + " x " + size.getHeight());
// Print all link URLs
List<WebElement> links = driver.findElements(By.tagName("a"));
for (WebElement link : links) {
String href = link.getAttribute("href");
System.out.println(href);
}
// Click accordion header
WebElement accordionHeader = driver.findElement(By.xpath("//*[@id=\"main\"]/button[1]"));
// Navigate back, forward, refresh and navigate to URL
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.navigate().to("https://www.amazon.in/");
// Close driver
driver.close();
// Click radio button
WebElement radioButton = driver.findElement(By.xpath("//*[@id=\"icp-language-settings\"]/div[3]/div/label/i"));
// Click checkbox
WebElement checkBox = driver.findElement(By.xpath("//*[@id=\"BE_flight_form_wrapper\"]/div[3]/div[1]/div[1]/a/i"));
// Click download button
driver.findElement(By.xpath("//*[@id=\"downloadButton\"]")).click();
// Scroll to bottom
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");
// Wait for 10 seconds
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// Maximize window
driver.manage().window().maximize();
// Scroll into view
WebElement elementToView = driver.findElement(By.xpath("/html/body/div[1]/div[2]/div[2]/div[2]/div/div[5]"));
js.executeScript("arguments[0].scrollIntoView();", elementToView);
}
}
package sele;
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;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
public class Local {
public static void main(String[] args) throws InterruptedException {
// Setting up ChromeDriver
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Pavani\\Desktop\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Open website
driver.get("https://www.amazon.in/");
// Search for "Electronics"
WebElement elementToSendKeys = driver.findElement(By.xpath("//*[@id=\"twotabsearchtextbox\"]"));
elementToSendKeys.sendKeys("Electroincs");
WebElement selectElement = driver.findElement(By.xpath("//*[@id=\"searchDropdownBox\"]"));
Select select = new Select(selectElement);
select.selectByVisibleText("Electronics");
WebElement elementToClick = driver.findElement(By.xpath("//*[@id=\"nav-search-submit-button\"]"));
// Perform actions
WebElement menuItem = driver.findElement(By.xpath("//*[@id=\"nav-link-accountList\"]/span"));
Actions actions = new Actions(driver);
actions.moveToElement(menuItem).perform();
WebElement element = driver.findElement(By.xpath("//*[@id=\"nav-flyout-ya-newCust\"]/a"));
actions.doubleClick(element).build().perform();
// Fill in and copy-paste fields
WebElement input1 = driver.findElement(By.xpath("//*[@id=\"ap_customer_name\"]"));
WebElement input2 = driver.findElement(By.xpath("//*[@id=\"ap_phone_number\"]"));
input1.sendKeys("Sai Ganesh");
Actions act = new Actions(driver);
act.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
act.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
act.sendKeys(Keys.TAB).perform();
act.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();
}
}
1. Calendar Code:
package sele;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
public class Calenders {
public static void main(String[] args) throws InterruptedException {
// Setting up ChromeDriver
System.setProperty("webdriver.chrome.driver", "C:\\Users\\Pavani\\Desktop\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Specify the test parameters
String testUrl = "https://jqueryui.com/datepicker/#date-range";
String expectedFromDateStr = "01/20/2020";
String expectedToDateStr = "06/20/2020";
// Open the website
driver.get(testUrl);
WebElement frame = driver.findElement(By.xpath("//*[@id='content']/iframe"));
driver.switchTo().frame(frame);
// Select the from date
WebElement fromDp = driver.findElement(By.xpath("//*[@id=\"from\"]"));
fromDp.click();
Thread.sleep(5000);
// Continue with selection
Select selectedFromMonth = new Select(driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div[1]/div/div/select")));
selectedFromMonth.selectByVisibleText("Jan");
// Select the day
WebElement fromDay = driver.findElement(By.xpath("//td[not(contains(@class,'ui-datepicker-month'))]/a[text()='20']"));
Thread.sleep(10000);
// Continue with selection of to date
WebElement toDp = driver.findElement(By.xpath("//*[@id=\"to\"]"));
toDp.click();
Thread.sleep(5000);
// Select month
Select selectedToMonth = new Select(driver.findElement(By.xpath("//*[@id=\"ui-datepicker-div\"]/div[1]/div/div/select")));
selectedToMonth.selectByVisibleText("Jun");
Thread.sleep(5000);
// Select day
WebElement toDay = driver.findElement(By.xpath("//td[not(contains(@class,'ui-datepicker-month'))]/a[text()='20']"));
toDay.click();
Thread.sleep(10000);
// Verification
String selectedFromDateStr = fromDp.getAttribute("value");
if(!selectedFromDateStr.equals(expectedFromDateStr)){
System.out.println("From date does not match expected value");
}
String selectedToDateStr = toDp.getAttribute("value");
if(!selectedToDateStr.equals(expectedToDateStr)){
System.out.println("To date does not match expected value");
}
System.out.println("Unit Test of jQuery Calendar passed");
driver.quit();
}
}
2. Table Code:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import java.util.List;
public class Calenders {
public static void main(String[] args) {
// Setting up ChromeDriver
System.setProperty("webdriver.chrome.driver","C:\\Users\\Pavani\\Desktop\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Open website
driver.get("https://www.w3schools.com/html/html_tables.asp");
// Read and print table
WebElement table = driver.findElement(By.xpath("//*[@id=\"customers\"]"));
List<WebElement> rows = table.findElements(By.tagName("tr"));
for (WebElement row : rows) {
List<WebElement> columns = row.findElements(By.tagName("td"));
for (WebElement column : columns) {
System.out.print(column.getText() + " ");
}
System.out.println();
}