(9-14)
9 - Radio Buttons and Checkboxes in Selenium WebdriverRadio Buttons and Checkboxes in Selenium Webdriver
Code:
import java.util.Collections;
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;
public class Doperation {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver = null;
driver = new ChromeDriver();
driver.get("https://www.hyrtutorials.com/p/frames-practice.html");
driver.manage().window().maximize();
driver.switchTo().frame("frm2");
driver.findElement(By.id("firstName")).sendKeys("STQA");
driver.findElement(By.id("lastName")).sendKeys("Lab");
WebElement
wlmale=driver.findElement(By.id("malerb"));
wlmale.click();
System.out.println("Radio button male clicked");
WebElement
wlfmale=driver.findElement(By.id("femalerb"));
wlmale.click();
System.out.println("Radio button female clicked");
List <WebElement> raditems=
driver.findElements(By.name("gender"));
System.out.println("Elements are");
for(int i=0;i< raditems.size(); i++ )
{
System.out.println(raditems.get(i).getAttribute("id")
);
}
List<WebElement> checkboxes =
driver.findElements(By.xpath("//input[@type='checkbox']"));
Collections.shuffle(checkboxes);
for (int i = 0; i < 3; i++) {
checkboxes.get(i).click();
System.out.println("Checkbox with ID: " +
checkboxes.get(i).getAttribute("id") + " clicked.");
}
}
}
10 - Automation of Registration Form
Code:
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 Regiform {
public static void main(String[] args) {
// TODO Auto-generated method stub
WebDriver driver;
driver = new ChromeDriver();
driver.get("https://www.hyrtutorials.com/p/css-selectors-practice.html");
driver.manage().window().maximize();
driver.findElement(By.id("firstName")).sendKeys("Anna");
driver.findElement(By.id("lastName")).sendKeys("Forst");
driver.findElement(By.className("gender")).sendKeys("Others");
driver.findElement(By.className("city")).sendKeys("Mumbai");
driver.findElement(By.cssSelector("#post-body-6773323888822088655 > div > form > div.container > input[type=text]:nth-child(5)")).sendKeys("India");
driver.findElement(By.cssSelector("#post-body-6773323888822088655 > div > form > div.container > input[type=text]:nth-child(6)")).sendKeys("How is your security?");
driver.findElement(By.xpath("//*[@id=\"post-body-6773323888822088655\"]/div/form/div[1]/input[7]")).sendKeys("Very Strong");
driver.findElement(By.xpath("//*[@id=\"post-body-6773323888822088655\"]/div/form/div[1]/input[8]")).sendKeys("Verified");
WebElement Wele = driver.findElement(By.cssSelector("#post-body-6773323888822088655 > div > form > div.container > select"));
Select sobj = new Select(Wele);
sobj.selectByValue("apple");
WebElement check = driver.findElement(By.cssSelector("#post-body-6773323888822088655 > div > form > div.container > input.confirm"));
check.click();
WebElement btn = driver.findElement(By.tagName("button"));
btn.click();
driver.close();
}
}
11 - Implement and Demonstrate Action Class in Selenium
Code:
import java.time.Duration;
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;
public class Mouse_Keyboard {
public static void main(String[] args) throws InterruptedException {
// TODO Auto-generated method stub
WebDriver driver=null;
driver=new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
//https://demo.automationtesting.in/Register.html -- for practice
driver.get("https://www.saucedemo.com/v1/");
driver.findElement(By.id("user-name")).sendKeys("standard_user");
driver.findElement(By.id("password")).sendKeys("secret_sauce");
driver.findElement(By.id("login-button")).click();
WebElement wl=driver.findElement(By.id("item_3_title_link"));
Actions actionobj1 = new Actions(driver);
// Perform Click Action on the Web Element normal click , right click double click
// actionobj1.click(wl).build().perform(); // normal click
// Duration time =actionobj1.contextClick(wl).getActionDuration(); // right click
// System.out.println("Time taken to perform the action " + time);
// actionobj1.doubleClick(wl).build().perform(); // Double click
/*driver.navigate().to("https://demoqa.com/buttons"); //Navigating to another page
WebElement wlD=driver.findElement(By.id("doubleClickBtn"));
Actions actionobj = new Actions(driver);
actionobj.doubleClick(wlD).build().perform();
WebElement wlR=driver.findElement(By.id("rightClickBtn"));
actionobj.contextClick(wlR).build().perform(); */
//Perform Mouse Hover Action on the Web Element
/* driver.navigate().to("https://demoqa.com/links");
Actions actionobjmh = new Actions(driver);
WebElement wlmh=driver.findElement(By.linkText("Home"));
actionobjmh.moveToElement(wlmh).build().perform();
System.out.println("Mouse hover Action performed"); */
// Perform drag & Drop operation on a webelement
/* driver.get("https://demo.automationtesting.in/Static.html");
// Create an instance of the Actions class
Actions action = new Actions(driver);
// Locate web source element that is a image
WebElement source= driver.findElement(By.id("mongo"));
// Locate target web element that is a box
WebElement target= driver.findElement(By.id("droparea"));
//perform the drag and drop action drag image and put in a box
action.dragAndDrop(source, target).build().perform(); */
// Perfrom click hold and release operation
/*driver.get("https://demo.automationtesting.in/Static.html");
// Create an instance of the Actions class
Actions action1 = new Actions(driver);
// Locate web source element that is a image
WebElement source1 = driver.findElement(By.id("angular"));
// Locate target web element that is a box
WebElement target1 = driver.findElement(By.id("droparea"));
// click a image hold it and move to box and realse the image in box
action1.clickAndHold(source1).moveToElement(target1).release().build().perform(); */
// keyboard key press up and down -> shift ,control, delete, backspace
/*driver.get("https://demoqa.com/automation-practice-form");
Actions action = new Actions(driver);
WebElement id = driver.findElement(By.id("firstName"));
action.keyDown(Keys.SHIFT).perform();
id.sendKeys("Inshirah");
System.out.println("name Entered");
Thread.sleep(1000);
action.keyUp(Keys.SHIFT).perform();
action.keyDown(Keys.CONTROL).sendKeys("a").keyUp(Keys.CONTROL).sendKeys(Keys.BACK_SPACE).perform(); */
// Upload any file to the server
String baseUrl = "https://demo.guru99.com/test/upload/";
driver.get(baseUrl);
WebElement uploadElement = driver.findElement(By.id("uploadfile_0"));
// enter the file path onto the file-selection input field
uploadElement.sendKeys("E:\\Users\\mca23_001\\Documents\\sample.jpg");
// check the "I accept the terms of service" check box
driver.findElement(By.id("terms")).click();
// click the "UploadFile" button
driver.findElement(By.name("send")).click();
System.out.println("File Uploaded Sucessfully");
}}
12 - Implementation of Annotations in testNG
Code:
package testng;
import org.testng.annotations.Test;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.AfterSuite;
public class AnnoApp {
WebDriver wd;
@Test(priority =0)
public void f() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.id("react-burger-menu-btn")).click();
wd.findElement(By.xpath("//*[@id=\"react-burger-cross-btn\"]")).click();
}
@Test(priority =1)
public void f1() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
Select s = new Select(wd.findElement(By.className("product_sort_container")));
s.selectByIndex(1);
}
@Test(priority =2)
public void f2() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
Select s = new Select(wd.findElement(By.className("product_sort_container")));
s.selectByIndex(2);
}
@Test(priority =3)
public void f3() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
Select s = new Select(wd.findElement(By.className("product_sort_container")));
s.selectByIndex(3);
}
@Test(priority =4)
public void f4() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.xpath("//*[@id=\"item_5_title_link\"]/div")).click();
wd.findElement(By.id("back-to-products")).click();
}
@Test(priority =5)
public void f5() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.xpath("//*[@id=\"item_4_title_link\"]/div")).click();
wd.findElement(By.id("back-to-products")).click();
}
@Test(priority =6)
public void f6() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.xpath("//*[@id=\"item_0_title_link\"]/div")).click();
wd.findElement(By.id("back-to-products")).click();
}
@Test(priority =7)
public void f7() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.xpath("//*[@id=\"item_1_title_link\"]/div")).click();
wd.findElement(By.id("back-to-products")).click();
}
@Test(priority =8)
public void f8() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.xpath("//*[@id=\"item_2_title_link\"]/div")).click();
wd.findElement(By.id("back-to-products")).click();
}
@Test(priority =9)
public void f9() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.xpath("//*[@id=\"item_3_title_link\"]/div")).click();
wd.findElement(By.id("back-to-products")).click();
}
@Test(priority =10)
public void f10() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.id("add-to-cart-sauce-labs-backpack")).click();
wd.findElement(By.id("remove-sauce-labs-backpack")).click();
}
@Test(priority =11)
public void f11() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.id("add-to-cart-sauce-labs-bike-light")).click();
wd.findElement(By.className("shopping_cart_link")).click();
wd.findElement(By.id("remove-sauce-labs-bike-light")).click();
wd.findElement(By.id("continue-shopping")).click();
}
@Test(priority =12)
public void f13() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.id("add-to-cart-sauce-labs-bike-light")).click();
wd.findElement(By.className("shopping_cart_link")).click();
wd.findElement(By.xpath("//*[@id=\"checkout\"]")).click();
wd.findElement(By.id("cancel")).click();
}
@Test(priority =13)
public void f14() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.xpath("//*[@id=\"checkout\"]")).click();
wd.findElement(By.id("first-name")).sendKeys("Hrishikesh");
wd.findElement(By.id("last-name")).sendKeys("Shinde");
wd.findElement(By.id("postal-code")).sendKeys("400708");
wd.findElement(By.id("continue")).click();
}
@Test(priority =14)
public void f15() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.id("finish")).click();
wd.findElement(By.id("back-to-products")).click();
}
@BeforeMethod
public void beforeMethod() {
}
@AfterMethod
public void afterMethod() {
}
@BeforeClass
public void beforeClass() {
wd.get("http://saucedemo.com");
wd.findElement(By.xpath("/html/body/div/div/div[2]/div[1]/div/div/form/div[1]/input")).sendKeys("standard_user");
wd.findElement(By.xpath("//*[@id=\"password\"]")).sendKeys("secret_sauce");
wd.findElement(By.xpath("//*[@id=\"login-button\"]")).click();
}
@AfterClass
public void afterClass() {
wd.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
wd.findElement(By.id("react-burger-menu-btn")).click();
wd.findElement(By.linkText("Logout")).click();
}
@BeforeTest
public void beforeTest() {
}
@AfterTest
public void afterTest() {
}
@BeforeSuite
public void beforeSuite() {
System.setProperty("webdriver.chrome.driver","E:\\Chrome Driver- Selenium\\117.0.5938.63\\chromedriver.exe" );
wd = new ChromeDriver();
}
@AfterSuite
public void afterSuite() {
}
}
13 - DataProvider Annotation
Code:
package ggg;
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.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class gh{
@Test(dataProvider="dp")
public void f(String un, String ps)
{
WebDriver driver=null;
driver = new ChromeDriver();
driver.get("https://www.saucedemo.com/v1/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));
driver.findElement(By.id("user-name")).sendKeys(un);
driver.findElement(By.id("password")).sendKeys(ps);
driver.findElement(By.id("login-button")).click();
try {
driver.findElement(By.xpath("//*[@id=\"inventory_container\"]/div/div[1]/div[3]/button")).click();
System.out.println("Testing Sucessful");
} catch (Exception e)
{
System.out.println("Testing Sucessful");
}
}
@DataProvider
/* public Object[][] dp()
{
Object[][] data = new Object[2][2];
{
data [0][0] ="standard_user";
data [0][1]="secret_sauce";
data [1][0] ="user";
data [1][1]="secret_sauce";
}
return data;
} */
public Object[][] dp()
{
return new Object[][]
{
new Object[]{"standard_user","secret_sauce"},
{ "abc","abc"},
{ "abcd","abcd"}
};
}
}
14 - Validation Check in TestNG
Code:
package demowork;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;
import org.testng.Assert;
public class annotationdemo{
@Test
public void validcheck()
{
WebDriver driver=null;
driver=new ChromeDriver();
driver.get("https://demo.automationtesting.in/Register.html");
WebElement wlps1=driver.findElement(By.id("firstpassword"));
wlps1.sendKeys("Sandeep");
WebElement wlps2=driver.findElement(By.id("secondpassword"));
wlps2.sendKeys("Yadav");
String ps1=wlps1.getText();
String ps2=wlps2.getText();
boolean result=ps1.equalsIgnoreCase(ps2);
System.out.println(result);
Assert.assertTrue(result);
if (result)
{
System.out.println(" Password is correct");
}
}
}