Wednesday, April 29, 2020

RealTime Example of @Before & @AfterMethod

package com.testng;

import java.sql.Driver;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class BeforeMethodAfterMethodTest {
WebDriver driver;
@BeforeMethod
public void before_config() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Desktop\\Selenium_software\\chromedriver.exe\\");
driver=new ChromeDriver();
driver.get("https://ingenioustechhub.blogspot.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);

}
@Test
public void verifyHomePageText_Tc_1() {
String actualText=driver.findElement(By.xpath("//h1[contains(text(),'Ingenious TechHub ')]")).getText();
System.out.println("actualText..."+ actualText);
String expText="Ingenious TechHub";
System.out.println("expText..."+ expText);
if(actualText.equals(expText)) {
System.out.println("verifyHomePageText_Tc_1 Passed");
}
else {
System.out.println("verifyHomePageText_Tc_1 failed");
}}

@Test
public void verifyTitlePage_tc_2() {
String actTitle=driver.getTitle();
System.out.println("actualTitle.."+ actTitle);
String expTitle="Ingenious TechHub";
System.out.println("expTitle...."+ expTitle);
if(actTitle.equals(expTitle)) {
System.out.println("verifySearchtextbox_tc_2 is passed");
}
else {
System.out.println("verifySearchtextbox_tc_2 is failed");
}

}

@AfterMethod
public void after_config() {
driver.close();
}

}

Sunday, April 26, 2020

How to Handle Dynamic Table

package com.seleniumtest;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebTableTest {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Desktop\\Selenium_software\\chromedriver.exe\\");
WebDriver driver=new ChromeDriver();
driver.get("https://www.makemytrip.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//input[@id='departure']/preceding::span[1]")).click();
Thread.sleep(1000);
List<WebElement> listtable=driver.findElements(By.xpath("//div[@class='DayPicker-NavBar']/following::div[2]//p"));
for(int i=0;i<listtable.size();i++) {
String str=listtable.get(i).getText();
System.out.println("strList...."+str);
if(str.equals("28")) {
listtable.get(i).click();
break;

}
}
System.out.println("Test case is passed");
}}
======================================================================
Example 2:- 
package com.seleniumtest;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;

public class WebTableTest1 {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Desktop\\Selenium_software\\chromedriver.exe\\");
WebDriver driver=new ChromeDriver();
driver.get("https://www.redbus.in/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[@id='togglebtn']/following::div[4]/span")).click();
List<WebElement> listOfTable=driver.findElements(By.xpath("//div[@id='rb-calendar_onward_cal']/table/tbody//td"));
for(int i=0;i<listOfTable.size();i++) {
String strLit=listOfTable.get(i).getText();
System.out.println("strLit..."+ strLit);
if(strLit.equals("28")) {
listOfTable.get(i).click();
break;
}
}
System.out.println("Test case passed");
driver.close();

}

}
========================================================================
Add caption

How to Handle Multiple Window In Selenium

Example 1:-
package com.seleniumpractice;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class HandleMultipleWindow {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Desktop\\Selenium_software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.naukri.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
String parentWindow=driver.getWindowHandle();
System.out.println("Parent window Title.."+driver.getTitle());
Set<String> set=driver.getWindowHandles();
Iterator<String> itr=set.iterator();
while(itr.hasNext()) {
String childWindow= itr.next();
if(!(parentWindow.equals(childWindow))) {
driver.switchTo().window(childWindow);
System.out.println("Child Window Title"+driver.getTitle());
}


}
   driver.switchTo().window(parentWindow);
   driver.close();
   System.out.println("Test case is passed");

}

}
========================================================================
Modify above code
------------------------
package com.seleniumpractice;

import java.sql.Driver;
import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MultipleWindowtest {

static WebDriver  driver;
public static void config() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Desktop\\Selenium_software\\chromedriver.exe\\");
driver=new ChromeDriver();
driver.get("https://www.naukri.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}
public static void main(String[] args) {
config();
String parentWindow=driver.getWindowHandle();
System.out.println("Parent Window Title..."+driver.getTitle());
Set<String> set=driver.getWindowHandles();
Iterator<String> itr=set.iterator();
while(itr.hasNext()) {
String childWindow=itr.next();
if(!(parentWindow.equals(childWindow))) {
driver.switchTo().window(childWindow);
System.out.println("Childwindow Title..."+ driver.getTitle());
}
}

driver.switchTo().window(parentWindow);
driver.quit();
System.out.println("****Test Case passed****");



}

}





========================================================================
Example:-2
package com.seleniumpractice;

import java.util.Iterator;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class MultileWindowCityBank {

public static void main(String[] args) {
     
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Desktop\\Selenium_software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://www.online.citibank.co.in/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
String parentWindow=driver.getWindowHandle();
System.out.println("parentWindowTitle..."+driver.getTitle());
driver.findElement(By.xpath("//a[@id='loginId']/img")).click();
Set<String>  set=driver.getWindowHandles();
Iterator<String> itr=set.iterator();
while(itr.hasNext()) {
String childwindow=itr.next();
if(!(parentWindow.equals(childwindow))) {
driver.switchTo().window(childwindow);
String actualText=driver.findElement(By.xpath("//h1[contains(text(),'Welcome to Citibank Online')]")).getText();
System.out.println("actualText Value.."+actualText);
String expText="Welcome to Citibank Online";
System.out.println("expTextValue.."+expText);
if(actualText.equals(expText)) {
System.out.println("Test case passed");
}
else {
System.out.println("Test case is not passed");
}
}
}
driver.switchTo().window(parentWindow);
}

}

Saturday, April 25, 2020

How to Handle Alert PopUp In Selenium

package com.seleniumpractice;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.Alert;
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;

public class AltertDemo {

public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver","C:\\Users\\admin\\Desktop\\Selenium_software\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://artoftesting.com/sampleSiteForSelenium");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
WebElement ele=driver.findElement(By.id("dblClkBtn"));
Actions act=new Actions(driver);
act.doubleClick(ele).perform();
Alert alt=driver.switchTo().alert();
Thread.sleep(2000);
String actualstr=alt.getText();
System.out.println("String value..."+actualstr);
String expStr="Hi! Art Of Testing, Here!";
if(actualstr.equals(expStr)){
alt.accept();
System.out.println("Test case pased");
}
else {
System.out.println("Test case failed");
}
//alt.accept();
//driver.close();

}

}
========================================================================

How to Handle Dynamic Frame in Selenium WebDrier

package com.seleniumpract;

import java.sql.Driver;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class PayTtmDemo {

public static void main(String[] args) {
System.setProperty("webdriver.chrome.driver","D:\\Ingenious_TechHub_Teaching\\Ingenious_Selenium\\driver\\chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("https://paytm.com/");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
driver.findElement(By.xpath("//div[contains(text(),'Log In/Sign Up')]")).click();
int frameSize=driver.findElements(By.tagName("iframe")).size();
System.out.println("frameSize.."+frameSize);
for(int i=0;i<frameSize;i++){
driver.switchTo().frame(i);

if(driver.findElements(By.xpath("//span[contains(text(),'Open Paytm ')]")).size()!=0){
       String actualStr= driver.findElement(By.xpath("//p[contains(text(),'Benefits of Paytm Account')]")).getText();
System.out.println("Actual Value.."+actualStr);
String expcedStr="Benefits of Paytm Account";
System.out.println("expcedStr.."+expcedStr);
if(actualStr.equals(expcedStr)){
System.out.println("Text is matched");
}
else{
System.out.println("Test is not Matched");
}
}
driver.switchTo().defaultContent();
System.out.println("Test case passed");
driver.close();



}




}

}
========================================================================

Q) How To Find Duplicate Characters In A String In Java?

Step1:- Creating a HashMap containing char as key and it's occurrences as value. Step2:- Converting given string to char array. Step3:- ...