S Det Selenium Imp File
S Det Selenium Imp File
S Det Selenium Imp File
xml
<modelVersion>4.0.0</modelVersion>
<groupId>seleniumproject</groupId>
<artifactId>seleniumproject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>io.github.bonigarcia</groupId>
<artifactId>webdrivermanager</artifactId>
<version>5.3.0</version>
</dependency>
</dependencies>
</project>
2.OrangeHRMLoginTest
package day17;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
/*
1) Launch browser
7) close browser
*/
import org.openqa.selenium.chrome.ChromeDriver;
//System.setProperty("webdriver.chrome.driver","C:\\Drivers\\chromedriver_win32\\chromedri
ver.exe");
//WebDriverManager.chromedriver().setup();
driver.get("https://opensource-demo.orangehrmlive.com/");
Thread.sleep(5000);
//3) Provide username - Admin
//WebElement txtUsername=driver.findElement(By.name("username"));
//txtUsername.sendKeys("Admin");
driver.findElement(By.name("username")).sendKeys("Admin");
driver.findElement(By.name("password")).sendKeys("admin");
driver.findElement(By.xpath("//*[@id=\"app\"]/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/b
utton")).click();
Thread.sleep(7000);
//Title validation
/*String act_title=driver.getTitle();
String exp_title="OrangeHRM";
if(act_title.equals(exp_title))
System.out.println("Test passed");
else
System.out.println("failed");
*/
try
{
act_label=driver.findElement(By.xpath("//*[@id='app']/div[1]/div[1]/header/div[1]/div[1]/span/h
6")).getText();
catch(NoSuchElementException e) {
String exp_label="Dashboard";
if(act_label.equals(exp_label))
System.out.println("test passed");
else
System.out.println("test failed");
//driver.close();
driver.quit();
3.LocatorsDemo1
package day18;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
//Open app
driver.get("http://automationpractice.com/index.php");
//search box
driver.findElement(By.id("search_query_top")).sendKeys("T-shirts");
//search button
driver.findElement(By.name("submit_search")).click();
driver.findElement(By.partialLinkText("Printed Chiffon")).click()}}
4.LocatorsDemo2
package day18;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
//Open app
driver.get("http://automationpractice.com/index.php");
List<WebElement> sliders=driver.findElements(By.className("homeslider-container"));
System.out.println("Number of sliders:"+sliders.size());
List<WebElement> images=driver.findElements(By.tagName("img"));
List<WebElement> links=driver.findElements(By.tagName("a"));
driver.quit();
}
5.CssSelectorsDemo
package day19;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://demo.nopcommerce.com/");
driver.manage().window().maximize();
//driver.findElement(By.cssSelector("input#small-searchterms")).sendKeys("MacBook");
//driver.findElement(By.cssSelector("#small-searchterms")).sendKeys("MacBook");
//tag class
//driver.findElement(By.cssSelector("input.search-box-text")).sendKeys("MacBook");
//driver.findElement(By.cssSelector(".search-box-text")).sendKeys("MacBook");
//driver.findElement(By.cssSelector("input[name='q']")).sendKeys("MacBook");
//driver.findElement(By.cssSelector("[name='q']")).sendKeys("MacBook");
driver.findElement(By.cssSelector("input.search-box-
text[name='q']")).sendKeys("MacBook");
}}
6.XpathDemo
package day20;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
WebDriver driver=new ChromeDriver();
driver.get("https://demo.opencart.com/");
driver.manage().window().maximize();
//String
productname=driver.findElement(By.xpath("/html[1]/body[1]/main[1]/div[2]/div[1]/div[1]/div[2]/div
[1]/form[1]/div[1]/div[2]/div[1]/h4[1]/a[1]")).getText();
System.out.println(productname);
7.XpathAxes
package day21;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class XPathAxes {
WebDriverManager.chromedriver().setup();
driver.get("https://money.rediff.com/gainers/bse/daily/groupa");
driver.manage().window().maximize();
String text=driver.findElement(By.xpath("//a[contains(text(),'NIIT
Ltd')]/self::a")).getText();
text=driver.findElement(By.xpath("//a[contains(text(),'NIIT
Ltd')]/parent::td")).getText();// there is no text for parent, so it is same value display
List<WebElement> childs=driver.findElements(By.xpath("//a[contains(text(),'NIIT
Ltd')]/ancestor::tr/child::td"));
text=driver.findElement(By.xpath("//a[contains(text(),'NIIT Ltd')]/ancestor::tr")).getText();
System.out.println("Ancestor : "+text);
//Descendant - Selects all descendants (children, grandchildren, etc.) of the current node
List<WebElement> descendants=driver.findElements(By.xpath("//a[contains(text(),'NIIT
Ltd')]/ancestor::tr/descendant::*"));
System.out.println("Number of descendant nodes:"+descendants.size());
//Following -Selects everything in the document after the closing tag of the current node
List<WebElement>followingnodes=driver.findElements(By.xpath("//a[contains(text(),'NIIT
Ltd')]/ancestor::tr/following::tr"));
List<WebElement>
followingsiblings=driver.findElements(By.xpath("//a[contains(text(),'NIIT Ltd')]/ancestor::tr/following-
sibling::tr"));
//Preceding - Selects all nodes that appear before the current node in the document
List<WebElement> precedings=driver.findElements(By.xpath("//a[contains(text(),'NIIT
Ltd')]/ancestor::tr/preceding::tr"));
List<WebElement>
precedingsiblings=driver.findElements(By.xpath("//a[contains(text(),'NIIT Ltd')]/ancestor::tr/preceding-
sibling::tr"));
driver.quit(); } }
8.GetMethods
package day22;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
/*get(url)
getTitle()
getCurrentURL()
getPageSource()
getWindowHandle()
getwindowHandles()
*/
WebDriverManager.chromedriver().setup();
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
driver.manage().window().maximize();
/*System.out.println("Page source......................");
String ps=driver.getPageSource();
System.out.println(ps);
System.out.println(ps.contains("html"));*/
//System.out.println(driver.getWindowHandle()); //CDwindow-
3127F2DCB717E0F5993100E70756C523
//CDwindow-46E50E8AE6529C7635BCF8E9EA2FB5DE
Thread.sleep(3000);
Set<String> windowids=driver.getWindowHandles();
for(String winid:windowids)
System.out.println(winid); //CDwindow-
B9429C1F5CC606A3981FAF0CD1A96DBC
//CDwindow-
339CE5C5C5FB731B207639DE22EC40D1
9.BrowserCommands
package day22;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(By.linkText("OrangeHRM, Inc")).click();
Thread.sleep(5000);
10.ConditionalCommands
package day22;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class ConditionalCommands {
WebDriverManager.chromedriver().setup();
driver.get("https://demo.nopcommerce.com/register");
driver.manage().window().maximize();
// isDisplayed() isEnabled()
//System.out.println(status);
//WebElement searchbox=driver.findElement(By.xpath("//input[@id='small-
searchterms']"));
//System.out.println("Dispay status:"+searchbox.isDisplayed());
//System.out.println("Enable status:"+searchbox.isEnabled());
//isSelected()
WebElement male_rd=driver.findElement(By.xpath("//input[@id='gender-male']"));
WebElement female_rd=driver.findElement(By.xpath("//input[@id='gender-female']"));
//Before selection
System.out.println("Before selection...............");
System.out.println(male_rd.isSelected()); //false
System.out.println(female_rd.isSelected()); //false
male_rd.click();
System.out.println(male_rd.isSelected()); //true
System.out.println(female_rd.isSelected()); //false
female_rd.click();
System.out.println(male_rd.isSelected()); //false
System.out.println(female_rd.isSelected()); //true
11.NavigationalCommands
package day23;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
driver.navigate().to("https://www.amazon.in/");
System.out.println(driver.getCurrentUrl());
driver.navigate().to("https://www.flipkart.com/");
System.out.println(driver.getCurrentUrl());
driver.navigate().back();
System.out.println(driver.getCurrentUrl()); // amzon
driver.navigate().forward();
System.out.println(driver.getCurrentUrl()); // flipkart
12.SleepStatement
package day23;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SleepStatement {
WebDriverManager.chromedriver().setup();
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
Thread.sleep(5000);
driver.findElement(By.xpath("//input[@placeholder='Username']")).sendKeys("Admin");
13.ImplicitWait
package day23;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
driver.findElement(By.xpath("//input[@placeholder='Username']")).sendKeys("Admin");
driver.findElement(By.xpath("//input[@placeholder='Password']")).sendKeys("admin123");
14.ExplicitWait
package day23;
import java.time.Duration;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
WebElement
username=mywait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@placeholder=
'Username']")));
useranme.sendKeys("Admin");
WebElement
password=mywait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@placeholder='
Password']")));
password.sendKeys("admin123");
}}
15.FluentWaitDemo
package day23;
import java.time.Duration;
import java.util.NoSuchElementException;
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.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import com.google.common.base.Function;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
mywait.withTimeout(Duration.ofSeconds(30));
mywait.pollingEvery(Duration.ofSeconds(5));
mywait.ignoring(NoSuchElementException.class);
//usage
WebElement username=(WebElement)
mywait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@placeholder='Username'
]")));
username.sendKeys("Admin");
16.Handling Checkboxes
package day24;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://itera-qa.azurewebsites.net/home/automation");
driver.manage().window().maximize();
//driver.findElement(By.xpath("//input[@id='monday']")).click();
List<WebElement> checkboxes=driver.findElements(By.xpath("//input[@class='form-
check-input' and @type='checkbox']"));
/*for(int i=0;i<checkboxes.size();i++)
checkboxes.get(i).click();
}*/
/*for(WebElement chkbox:checkboxes)
chkbox.click();
}*/
/*for(int i=4;i<checkboxes.size();i++)
checkboxes.get(i).click();
}*/
/*for(int i=0;i<3;i++)
{
checkboxes.get(i).click();
}*/
/*for(int i=0;i<checkboxes.size();i++)
if(i<2)
checkboxes.get(i).click();
}*/
//clear/Uncheck checkboxes
for(int i=0;i<3;i++)
checkboxes.get(i).click();
Thread.sleep(4000);
/*for(int i=0;i<checkboxes.size();i++)
if(checkboxes.get(i).isSelected())
checkboxes.get(i).click();
}*/
for(WebElement chkbox:checkboxes)
{
if(chkbox.isSelected())
chkbox.click();
package day24;
import java.time.Duration;
import java.util.List;
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;
import io.github.bonigarcia.wdm.WebDriverManager;
import io.opentelemetry.exporter.logging.SystemOutLogExporter;
WebDriverManager.chromedriver().setup();
driver.get("https://phppot.com/demo/jquery-dependent-dropdown-list-countries-and-
states/");
driver.manage().window().maximize();
WebElement drpCountryEle=driver.findElement(By.xpath("//select[@id='country-list']"));
//drpCountry.selectByVisibleText("USA");
//drpCountry.selectByIndex(3); //France
List<WebElement> options=drpCountry.getOptions();
/*for(int i=0;i<options.size();i++)
System.out.println(options.get(i).getText());
}*/
for(WebElement op:options)
System.out.println(op.getText());
}
}
package day24;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.jquery-az.com/boots/demo.php?ex=63.0_2");
driver.manage().window().maximize();
//clicking on the dropdown
driver.findElement(By.xpath("//button[contains(@class,'multiselect')]")).click();
List<WebElement>
options=driver.findElements(By.xpath("//ul[contains(@class,multiselect)]//label"));
/*for(int i=0;i<options.size();i++)
System.out.println(options.get(i).getText());
}*/
/*for(int i=0;i<options.size();i++)
String option=options.get(i).getText();
if(option.equals("Java") || option.equals("Python"))
options.get(i).click();
}*/
for(WebElement option:options)
{
String text=option.getText();
if(text.equals("Java") || text.equals("Python"))
option.click();
package day25;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.google.com/");
driver.manage().window().maximize();
driver.findElement(By.xpath("//input[@name='q']")).sendKeys("selenium");
Thread.sleep(3000);
List<WebElement>
list=driver.findElements(By.xpath("//div[contains(@class,'wM6W7d')]//span"));
System.out.println("Number of suggestions:"+list.size());
for(int i=0;i<list.size();i++)
String text=list.get(i).getText();
if(text.equals("selenium tutorial"))
list.get(i).click();
break;
}}
20.Handle Alerts
package day25;
import java.time.Duration;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
driver.manage().window().maximize();
//Alert alertwindow=driver.switchTo().alert();
Alert alertwindow=mywait.until(ExpectedConditions.alertIsPresent());
System.out.println(alertwindow.getText());
package day25;
import java.time.Duration;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://the-internet.herokuapp.com/javascript_alerts");
driver.manage().window().maximize();
Alert alertwindow=driver.switchTo().alert();
System.out.println(alertwindow.getText());
alertwindow.sendKeys("welcome");
alertwindow.accept();
//alertwindow.dismiss();
//validation
String act_text=driver.findElement(By.xpath("//p[@id='result']")).getText();
System.out.println(exp_text);
if(act_text.equals(exp_text))
System.out.println("test passed");
else
System.out.println("test failed");
22.Authenticated Popup
package day25;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
//http://the-internet.herokuapp.com/basic_auth
//http://admin:admin@the-internet.herokuapp.com/basic_auth
driver.get("http://admin:admin@the-internet.herokuapp.com/basic_auth");
String text=driver.findElement(By.xpath("//p[contains(text(),'Congratulations!')]")).getText();
if(text.contains("Congratulations"))
System.out.println("successful login");
else
System.out.println("login failed");
package day26;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
driver.manage().window().maximize();
driver.findElement(By.xpath("//a[normalize-space()='OrangeHRM, Inc']")).click();
/*
String parentWindowID=windowidsList.get(0);
String childWindowID=windowidsList.get(1);
driver.switchTo().window(childWindowID);
driver.findElement(By.xpath("//div[@class='d-flex web-menu-btn']//li[1]//a[1]")).click();
driver.switchTo().window(parentWindowID);
driver.findElement(By.xpath("//input[@placeholder='Username']")).sendKeys("admin");
*/
//Appraoch2
/*for(String winid:windowIDs)
String title=driver.switchTo().window(winid).getTitle();
driver.findElement(By.xpath("//div[@class='d-flex web-menu-
btn']//li[1]//a[1]")).click();
}*/
/*for(String winid:windowIDs)
String title=driver.switchTo().window(winid).getTitle();
if( title.equals("x") || title.equals("y") || title.equals("z"))
driver.close();
}*/
for(String winid:windowIDs)
String title=driver.switchTo().window(winid).getTitle();
driver.close();
package day26;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://www.selenium.dev/selenium/docs/api/java/index.html?overview-
summary.html");
driver.manage().window().maximize();
driver.switchTo().frame("packageListFrame");
driver.findElement(By.linkText("org.openqa.selenium")).click();// frame1
driver.switchTo().frame("packageFrame");
driver.findElement(By.linkText("WebDriver")).click(); // frame2
driver.switchTo().frame("classFrame");
driver.findElement(By.xpath("//div[@class='topNav']//a[normalize-
space()='Overview']")).click();
}
25.Handle Inner Frames Demo
package day26;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://ui.vision/demo/webtest/frames/");
driver.manage().window().maximize();
//Frame1
WebElement frm1=driver.findElement(By.xpath("//frame[@src='frame_1.html']"));
driver.switchTo().frame(frm1);
driver.findElement(By.xpath("//input[@name='mytext1']")).sendKeys("11111");
driver.switchTo().defaultContent();
//Frame3
WebElement frm3=driver.findElement(By.xpath("//frame[@src='frame_3.html']"));
driver.switchTo().frame(frm3);
driver.findElement(By.xpath("//input[@name='mytext3']")).sendKeys("33333");
//inner frame
driver.switchTo().frame(0);
driver.switchTo().defaultContent();
package day27;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
int cols=driver.findElements(By.xpath("//table[@name='BookTable']//th")).size();
//String
value=driver.findElement(By.xpath("//table[@name='BookTable']//tr[7]//td[1]")).getText();
//System.out.println(value); //Master In JS
//4) Read data from all the rows & columns
for(int r=2;r<=rows;r++)
for(int c=1;c<=cols;c++)
String
value=driver.findElement(By.xpath("//table[@name='BookTable']//tr["+r+"]//td["+c+"]")).getText();
System.out.print(value+"\t");
System.out.println();
*/
/*for(int r=2;r<=rows;r++)
String
author=driver.findElement(By.xpath("//table[@name='BookTable']//tr["+r+"]/td[2]")).getText();
if(author.equals("Mukesh"))
String
bookname=driver.findElement(By.xpath("//table[@name='BookTable']//tr["+r+"]/td[1]")).getText();
System.out.println(author+" "+bookname);
}
}*/
int sum=0;
for(int r=2;r<=rows;r++)
String
price=driver.findElement(By.xpath("//table[@name='BookTable']//tr["+r+"]/td[4]")).getText();
sum=sum+Integer.parseInt(price);
package day27;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class PaginationTable {
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://demo.opencart.com/admin/index.php");
driver.manage().window().maximize();
//Login
WebElement username=driver.findElement(By.id("input-username"));
username.clear();
username.sendKeys("demo"); //demo
WebElement password=driver.findElement(By.id("input-password"));
password.clear();
password.sendKeys("demo"); //demo
driver.findElement(By.xpath("//button[text()=' Login']")).click();
if(driver.findElement(By.xpath("//button[@class='btn-close']")).isDisplayed())
driver.findElement(By.xpath("//button[@class='btn-close']")).click();
//Customers--->customers
driver.findElement(By.xpath("//a[@class='parent collapsed'][normalize-
space()='Customers']")).click();
driver.findElement(By.xpath("//ul[@class='collapse
show']//a[contains(text(),'Customers')]")).click();
int total_pages=Integer.parseInt(text.substring(text.indexOf("(")+1,text.indexOf("Pages")-
1));
if(total_pages>1)
WebElement
active_Page=driver.findElement(By.xpath("//ul[@class='pagination']//li//*[text()="+p+"]"));
active_Page.click();
Thread.sleep(2000);
for(int r=1;r<=noOfrows;r++)
{
String
customerName=driver.findElement(By.xpath("//table[@class='table table-bordered table-
hover']//tbody//tr["+r+"]//td[2]")).getText();
String
customerEmail=driver.findElement(By.xpath("//table[@class='table table-bordered table-
hover']//tbody//tr["+r+"]//td[3]")).getText();
System.out.println(customerName+" "+customerEmail+"
"+status);
driver.quit();
package day28;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class DropDownWithHiddenOptions {
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://opensource-demo.orangehrmlive.com/");
driver.manage().window().maximize();
driver.findElement(By.name("username")).sendKeys("Admin");
driver.findElement(By.name("password")).sendKeys("admin123");
driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
//Dropwdown
driver.findElement(By.xpath("//div[6]//div[1]//div[2]//div[1]//div[1]//div[2]")).click();//
opens the dropdown
List<WebElement>
options=driver.findElements(By.xpath("//div[@role='listbox']//span"));
for(WebElement option:options)
//System.out.println(option.getText());
if(option.getText().equals("Finance Manager"))
{
option.click();
break;
}} }
package day28;
import java.util.Arrays;
String price="$200.98";
Arrays.sort(a);
System.out.println("smallest number:"+a[0]);
System.out.println("$"+a[0]);
}
package day28;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://jqueryui.com/datepicker/");
driver.manage().window().maximize();
//Appraoch 1
//driver.findElement(By.xpath("//input[@id='datepicker']")).sendKeys("10/15/2022"); //
mm/dd/yyyy
//Approach2
String year="2024";
String month="October";
String date="12";
while(true)
String mon=driver.findElement(By.xpath("//span[@class='ui-datepicker-
month']")).getText();
String yr=driver.findElement(By.xpath("//span[@class='ui-datepicker-
year']")).getText();
break;
}
//driver.findElement(By.xpath("//span[@class='ui-icon ui-icon-circle-triangle-
e']")).click(); // Future date
//driver.findElement(By.xpath("//span[@class='ui-icon ui-icon-circle-triangle-
w']")).click(); //Past date
//select date
List<WebElement> allDates=driver.findElements(By.xpath("//table[@class='ui-
datepicker-calendar']//td"));
for(WebElement dt:allDates)
if(dt.getText().equals(date))
dt.click();
break;
/*for(int i=0;i<allDates.size();i++)
if(allDates.get(i).getText().equals(date))
allDates.get(i).click();
break;
}*/
}
package day29;
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.interactions.Actions;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://demo.opencart.com/");
driver.manage().window().maximize();
WebElement desktops=driver.findElement(By.xpath("//a[normalize-
space()='Desktops']"));
WebElement mac=driver.findElement(By.xpath("//a[normalize-space()='Mac (1)']"));
//Mouse hover
//act.moveToElement(desktops).moveToElement(mac).click().build().perform();//
creating action then perform
act.moveToElement(desktops).moveToElement(mac).click().perform(); // directly
performing action
package day29;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
public class LocationOfElement {
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://opensource-demo.orangehrmlive.com/web/index.php/auth/login");
WebElement logo=driver.findElement(By.xpath("//img[@alt='company-branding']"));
driver.manage().window().maximize();
driver.manage().window().minimize();
driver.manage().window().fullscreen();
driver.manage().window().setPosition(p);
p=new Point(50,50);
driver.manage().window().setPosition(p);
System.out.println("After setting window 50 * 50:"+logo.getLocation()); //(252, 160)
package day29;
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.interactions.Actions;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("http://swisnl.github.io/jQuery-contextMenu/demo.html");
driver.manage().window().maximize();
WebElement button=driver.findElement(By.xpath("//span[@class='context-menu-one
btn btn-neutral']"));
Actions act=new Actions(driver);
//right click
act.contextClick(button).perform();
Thread.sleep(5000);
package day29;
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.interactions.Actions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class SliderDemo {
WebDriverManager.chromedriver().setup();
driver.get("https://www.jqueryscript.net/demo/Price-Range-Slider-jQuery-UI/");
driver.manage().window().maximize();
//Min slider
WebElement min_slider=driver.findElement(By.xpath("//span[1]"));
//Max slider
WebElement max_slider=driver.findElement(By.xpath("//span[2]"));
}
35. Drag and Drop Demo
package day29;
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.interactions.Actions;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("http://www.dhtmlgoodies.com/scripts/drag-drop-custom/demo-drag-drop-
3.html");
driver.manage().window().maximize();
Actions act=new Actions(driver);
// rome---> Italy
WebElement rome=driver.findElement(By.xpath("//div[@id='box6']"));
WebElement italy=driver.findElement(By.xpath("//div[@id='box106']"));
// Wasington---> USA
WebElement washington=driver.findElement(By.xpath("//div[@id='box3']"));
WebElement usa=driver.findElement(By.xpath("//div[@id='box103']"));
act.dragAndDrop(washington, usa).perform();
package day29;
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.interactions.Actions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class DoubleClickDemo {
WebDriverManager.chromedriver().setup();
driver.get("https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_ev_ondblclick3");
driver.manage().window().maximize();
WebElement f1=driver.findElement(By.xpath("//input[@id='field1']"));
f1.clear();
f1.sendKeys("Welcome");
WebElement button=driver.findElement(By.xpath("//button[normalize-space()='Copy
Text']"));
//validation
WebElement f2=driver.findElement(By.xpath("//input[@id='field2']"));
if(copiedtext.equals("Welcome"))
{
System.out.println("test passed");
else
System.out.println("test failed");
}}}
37.Actions vs Action
package day29;
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.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("http://swisnl.github.io/jQuery-contextMenu/demo.html");
driver.manage().window().maximize();
WebElement button=driver.findElement(By.xpath("//span[@class='context-menu-one
btn btn-neutral']"));
Actions act=new Actions(driver);
//act.contextClick(button).build().perform();
package day30;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WindowType;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
driver.manage().window().maximize();
driver.get("https://www.opencart.com/");
driver.get("https://opensource-demo.orangehrmlive.com/");
39.KeyBoard actions
package day30;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import io.github.bonigarcia.wdm.WebDriverManager;
public class KeyboardActions {
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://text-compare.com/");
driver.findElement(By.xpath("//textarea[@id='inputText1']")).sendKeys("WELCOME TO
AUTOMATION");
//ctrl +a
act.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).perform();
//ctrl +c
act.keyDown(Keys.CONTROL).sendKeys("c").keyUp(Keys.CONTROL).perform();
//tab
act.keyDown(Keys.TAB).keyUp(Keys.TAB).perform();
//ctrl+v
act.keyDown(Keys.CONTROL).sendKeys("v").keyUp(Keys.CONTROL).perform();
40.JavaScript Executor
package day30;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://testautomationpractice.blogspot.com/");
driver.manage().window().maximize();
driver.switchTo().frame(0);
JavascriptExecutor js=(JavascriptExecutor) driver;
//JavascriptExecutor js=driver;
WebElement inputbox=driver.findElement(By.id("RESULT_TextField-1"));
js.executeScript("arguments[0].setAttribute('value','john')", inputbox);
//Radio button
WebElement male_Rd=driver.findElement(By.id("RESULT_RadioButton-7_0"));
//male_Rd.click(); //ClickInterceptedException
js.executeScript("arguments[0].click();",male_Rd);
//Checkbox
WebElement chkbox=driver.findElement(By.id("RESULT_CheckBox-8_0"));
js.executeScript("arguments[0].click();",chkbox);
//button
WebElement button=driver.findElement(By.id("FSsubmit"));
js.executeScript("arguments[0].click();",button);
}}
41.Scrolling Page
package day30;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.get("https://www.countries-ofthe-world.com/flags-of-the-world.html");
driver.manage().window().maximize();
JavascriptExecutor js=driver;
//js.executeScript("window.scrollBy(0,3000)", "");
//js.executeScript("arguments[0].scrollIntoView();", flag);
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
System.out.println(js.executeScript("return window.pageYOffset;"));
Thread.sleep(3000);
// go back to initial position
js.executeScript("window.scrollBy(0,-document.body.scrollHeight)");
42.Broken Links
package day31;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.time.Duration;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("http://www.deadlinkcity.com/");
driver.manage().window().maximize();
List<WebElement> links=driver.findElements(By.tagName("a"));
int brokenlinks=0;
for(WebElement linkEle:links)
String hrefAttValue=linkEle.getAttribute("href");
if(hrefAttValue==null || hrefAttValue.isEmpty())
continue;
conn.connect();
if(conn.getResponseCode()>=400)
{
brokenlinks++;
else
package day31;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import io.github.bonigarcia.wdm.WebDriverManager;
WebDriverManager.chromedriver().setup();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://demo.nopcommerce.com/");
driver.manage().window().maximize();
/*TakesScreenshot ts=(TakesScreenshot)driver;
File src=ts.getScreenshotAs(OutputType.FILE);
File trg=new
File("C:\\Users\\pavan\\Aug19Batch\\seleniumproject\\screenshots\\fullpage.png");
FileUtils.copyFile(src, trg);
*/
/*WebElement featuredproducts=driver.findElement(By.xpath("//div[@class='product-
grid home-page-product-grid']"));
File src=featuredproducts.getScreenshotAs(OutputType.FILE);
File trg=new
File("C:\\Users\\pavan\\Aug19Batch\\seleniumproject\\screenshots\\featureproducts.png");
FileUtils.copyFile(src, trg);
*/
WebElement logo=driver.findElement(By.xpath("//img[@alt='nopCommerce
demo store']"));
File src=logo.getScreenshotAs(OutputType.FILE);
File trg=new
File("C:\\Users\\pavan\\Aug19Batch\\seleniumproject\\screenshots\\logo.png");
FileUtils.copyFile(src, trg);
44.Headless Testing
package day31;
import java.net.SocketException;
import java.time.Duration;
import java.util.NoSuchElementException;
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.chrome.ChromeOptions;
import org.openqa.selenium.edge.EdgeDriver;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.FluentWait;
import org.openqa.selenium.support.ui.Wait;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.google.common.base.Function;
import io.github.bonigarcia.wdm.WebDriverManager;
options.setHeadless(true);
WebDriverManager.chromedriver().setup();
*/
//options.setHeadless(true);
//WebDriver driver=WebDriverManager.chromedriver().capabilities(options).create();
//Appraoch1
options.setHeadless(true);
WebDriverManager.edgedriver().setup();
*/
//Appraoch 2
options.setHeadless(true);
WebDriver driver=WebDriverManager.edgedriver().capabilities(options).create();
driver.get("https://opensource-demo.orangehrmlive.com/");
Thread.sleep(3000);
driver.findElement(By.name("username")).sendKeys("Admin");
driver.findElement(By.name("password")).sendKeys("admin123");
driver.findElement(By.xpath("//*[@id='app']/div[1]/div/div[1]/div/div[2]/div[2]/form/div[3]/butt
on")).click();
// validation
String act_title=driver.getTitle();
String exp_title="OrangeHRM";
if(act_title.equals(exp_title))
else
driver.quit();