=======================================================
Working with SSL Certificates
============================================================================
EX1: SSL Certificate for firefox Driver Until 2.53.1 jars
package SSLCertificates;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class SSLFirefox
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
FirefoxProfile firefoxprofile = new FirefoxProfile();
firefoxprofile.setAssumeUntrustedCertificateIssuer(true);
firefoxprofile.setAcceptUntrustedCertificates(true);
WebDriver driver = new FirefoxDriver(firefoxprofile);
driver.get("https://axisbank.com");
driver.close();
driver.quit();
}
}
From 3.X onwards use FirefoxOptions class
package SSLCertificates;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
public class SSLFirefox
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.20.1-win32\\geckodriver.exe");
FirefoxOptions FFOptions = new FirefoxOptions();
FFOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new FirefoxDriver(FFOptions);
driver.get("https://axisbank.com");
driver.close();
driver.quit();
}
}
-------------------------------------------------------------------------------------------------------------------------
Ex2: SSL Certificates for Chrome Driver : Until 2.53.1
--------------------------------------------------------------------------------------
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SSLChrome
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(capability);
driver.get("https://axisbank.com/");
}
}
From 3.X onwards: Use ChromeOptions
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
public class SSLChrome
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\chromedriver_win321\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
ChromeOptions capability = new ChromeOptions();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(capability);
driver.get("https://axisbank.com/");
}
}
-------------------------------------------------------------------------------------------------------------------------
Ex2: SSL Certificates for IE Driver : Below example until 2.53.1
--------------------------------------------------------------------------------------
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SSLForIE
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.get("https://axisbank.com");
}
}
From 3.0 onwards : InternetExplorerOptions();
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.remote.CapabilityType;
public class SSLForIE
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\IEDriverServer_x64_3.12.0\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
InternetExplorerOptions caps =new InternetExplorerOptions();
//It will avoid security domain warning message
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
//It will accept ssl certificate
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.get("https://axisbank.com");
}
}
-------------------------------------------------------------------------------------------------------------------------
Ex4: Proxy server connection
--------------------------------------------------------------------------------------
package SSLCertificates;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Proxy;
public class ProxyForDriver
{
public static void main(String[] args)
{
String PROXY = "192.168.1.11:8080";
Proxy proxy = new Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);
}
}
==================================================================================
How To Download files from firefox Driver: Until 2.53.1
=======================================================
package Examples;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class DownLoad
{
public static void main(String[] args) throws Exception
{
/*
1. Create a PROFILE object of Browser.
2. Set Preference, by giving Download destination Directory.
3. Set Preference, by giving Default Folder.
0 => Desktop,
1=>System Default Location,
2 => Indicates a custom Folder Location
4. Set Preference, A comma-separated list of MIME types to save to disk without
asking what to use to open the file. Default value is an empty string.
*/
FirefoxProfile Prof = new FirefoxProfile();
Prof.setPreference("browser.download.dir", "C:\\Down");
Prof.setPreference("browser.download.folderList", 2);
Prof.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
WebDriver driver = new FirefoxDriver(Prof);
driver.manage().window().maximize();
driver.get("http://docs.seleniumhq.org/download/");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='mainContent']/p[7]/a[1]")).click();
}
}
From 3.X Onwards
===============================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class DownLoad
{
public static void main(String[] args) throws Exception
{
/*
1. Create a FirefoxOptions object of Browser.
2. Set Preference, by giving Download destination Directory(Folder).
3. Set Preference, by giving Default Folder.
0 => Desktop,
1=>System Default Location,Means download folder
2 => Indicates a custom Folder Location
4. Set Preference, A comma-separated list of MIME types to save to disk without
asking what to use to open the file. Default value is an empty string.
*/
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.20.1-win32\\geckodriver.exe");
FirefoxOptions Prof = new FirefoxOptions();
Prof.addPreference("browser.download.dir", "C:\\Down");
Prof.addPreference("browser.download.folderList", 2);
Prof.addPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
//--------------------------------------------------------------------
WebDriver driver = new FirefoxDriver(Prof);
//----------------------------------------------------------------------
driver.manage().window().maximize();
driver.get("http://docs.seleniumhq.org/download/");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='mainContent']/p[7]/a[1]")).click();
}
}
=============================================================
Navigate Commands Example
===============================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateCommands
{
public static void main(String[] args)throws Exception
{
FirefoxDriver Brow=new FirefoxDriver();
Brow.manage().window().maximize();
//---------------------------------------------
Brow.navigate().to("http://www.calculator.net/");
Thread.sleep(3000);
//Verify Home Page
if(Brow.getTitle().equals("Calculator.net: Free Online Calculators - Math, Health, Financial, Science"))
{
System.out.println("Calculator page displayed");
}
//click on All Calculators
Brow.findElement(By.xpath("//*[@id='hcalc']/table/tbody/tr/td[2]/p/a/b")).click();
Thread.sleep(1000);
//click on Due date
Brow.findElement(By.linkText("Due Date Calculator")).click();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
//Go back to previous page
Brow.navigate().back();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Sitemap"))
{
System.out.println("Sitemap page displayed");
}
//Go to next page
Brow.navigate().forward();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
Brow.close();
Brow.quit();
}
}
============================================================
How to use JavaScript Executor:
===========================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JavScriptExecEx
{
/*
JAVA SCRIPT example
We can use java script command to perform actions.
We can write code to fill up the text box through java script.
Please follow the steps mentioned below.
1. Create Driver for any Browser(Mozilla).
2. Go to the URL.
3. Create Java Script executor object for the Driver.
4. Store the Java Script command in a String Variable.
5. Java Script Executor object executes the command in the Variable.
*/
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://classroom:90/qahrm");
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsUN = "document.getElementsByName('txtUserName')[0].value='qaplanet1'";
js.executeScript(jsUN);
String jsPWD = "document.getElementsByName('txtPassword')[0].value='user1'";
js.executeScript(jsPWD);
driver.findElement(By.cssSelector("input.button")).click();
}
}
=============================================================================
How To find Particular link
=============================================================================
package Examples;
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.firefox.FirefoxDriver;
public class GetAllLinksName
{
public static void main(String[] args)throws Exception
{
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.get("http://www.calculator.net/");
driver.manage().window().maximize();
List<WebElement> list=driver.findElements(By.tagName("a"));
//System.out.println(list.size());
//int txt=list.size();
//System.out.println(txt);
System.out.println(list.size());
for(int i=0;i<list.size();i++)
{
String txt=list.get(i).getText();
System.out.println(txt);
if(txt.equals("Due Date Calculator"))
{
list.get(i).click();
break;
}
}
driver.close();
driver.quit();
}
}
=================================================================================
Auto Suggestion
================================================================================
import java.util.List;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
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.firefox.FirefoxDriver;
public class Autosugeestion
{
public static void main(String[] args)throws Exception
{
// System.setProperty("webdriver.chrome.driver", "F:\\D-Drive\\FromC\\chromedriver_win32\\chromedriver.exe");
// WebDriver driver =new ChromeDriver();
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.com");
WebElement E=driver.findElement(By.id("lst-ib"));
E.sendKeys("selenium");
//find UL with class property
WebElement ul = driver.findElement(By.className("sbsb_b"));
List<WebElement> Lic = ul.findElements(By.tagName("li"));
//-----------------------------------------------------------
//Using Iterator object
//------------------------------------------------------------------------
Iterator<WebElement> i = Lic.iterator();
System.out.println("-----------------------------------------");
while(i.hasNext())
{
Thread.sleep(1000);
WebElement LiI = i.next();
//WebElement E=LiI.findElement(By.xpath("//div[@id='sbse0']/div[2]"));
String data=LiI.findElement(By.cssSelector("div.sbqs_c")).getText();
System.out.println(data);
System.out.println("-----------------------------------------");
if(data.equals("selenium webdriver"))
{
LiI.click();
break;
}
}
//----------------------------------------------------------------
//Using for each
//--------------------------------------------------------------------
for (WebElement LI : Lic)
{
String itemname=LI.findElement(By.cssSelector("div.sbqs_c")).getText();
System.out.println(itemname);
if(itemname.equals("selenium webdriver"))
{
LI.click();
break;
}
}
Thread.sleep(1000);
}
}
=================================================================================
Navigate Commands Example
==================================================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateCommands
{
public static void main(String[] args)throws Exception
{
FirefoxDriver Brow=new FirefoxDriver();
Brow.manage().window().maximize();
//---------------------------------------------
Brow.navigate().to("http://www.calculator.net/");
Thread.sleep(3000);
//Verify Home Page
if(Brow.getTitle().equals("Calculator.net: Free Online Calculators - Math, Health, Financial, Science"))
{
System.out.println("Calculator page displayed");
}
//click on All Calculators
Brow.findElement(By.xpath("//*[@id='hcalc']/table/tbody/tr/td[2]/p/a/b")).click();
Thread.sleep(1000);
//click on Due date
Brow.findElement(By.linkText("Due Date Calculator")).click();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
//Go back to previous page
Brow.navigate().back();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Sitemap"))
{
System.out.println("Sitemap page displayed");
}
//Go to next page
Brow.navigate().forward();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
Brow.close();
Brow.quit();
}
}
=================================================================================
How To Get Tool Tip
=================================================================================
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ToolTipEx
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver Brow = new FirefoxDriver();
Brow.manage().window().maximize();
Brow.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
//------------------------------------------------------------------
Brow.get("http://www.google.co.in/");
//Get Title attribute of Edit box
String ToolTip=Brow.findElement(By.id("lst-ib")).getAttribute("title");
System.out.println(ToolTip);
//close browser
Brow.close();
//quit object
Brow.quit();
}
}
====================================================================================
==================================================================================
How To take screenshot
=====================================================================================
import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;
public class Screenshot
{
public static void main(String[] args)throws Exception
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://classroom:90/qahrm");
File scrFile = ((TakesScreenshot)driver).
getScreenshotAs(OutputType.FILE);
// Now copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\screenshot.png"));
driver.close();
driver.quit();
}
}
===========================================================================
Working With Multiple Browsers
==========================================================================
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class ToWorWithMultipleBrowsersk
{
public static void main(String[] args)throws Exception
{
System.setProperty("webdriver.chrome.driver", "F:\\D-Drive\\FromC\\chromedriver_win321\\chromedriver.exe");
ChromeDriver Driver = new ChromeDriver();
Driver.manage().window().maximize();
//-----------------------------------------------
Driver.get("http://qaplanet.in/");
Thread.sleep(2000);
//Click on Linked icon
Driver.findElement(By.xpath("//*[@id='section-tophat']/div[1]/div/div[2]/ul/li[4]/a/img")).click();
Thread.sleep(4000);
Set<String> IDS=Driver.getWindowHandles();
//Way 1
for(String ID : IDS)
{
System.out.println(ID);
}
System.out.println("----------------------------------------");
//Way 2
Iterator<String> ITR=IDS.iterator();
String First_ID=ITR.next();
String Second_ID=ITR.next();
//Switch to second browser
Driver.switchTo().window(Second_ID);
Thread.sleep(4000);
//click on Sign in
Driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(2000);
//Enter email
Driver.findElement(By.name("session_key")).sendKeys("qaplanet@gmail.com");
Thread.sleep(2000);
//close browser
Driver.close();
Thread.sleep(2000);
//--------------------------------------------------------------
//Switch to parent browser
Driver.switchTo().window(First_ID);
//Execute code
Thread.sleep(2000);
//Click on justdial icon
Driver.findElement(By.xpath("//*[@id='section-tophat']/div[1]/div/div[2]/ul/li[5]/a/img")).click();
//Execute code
Thread.sleep(3000);
//---------------------------------------------
Driver.close();
Driver.quit();
}
}
Working with SSL Certificates
============================================================================
EX1: SSL Certificate for firefox Driver Until 2.53.1 jars
package SSLCertificates;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class SSLFirefox
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
FirefoxProfile firefoxprofile = new FirefoxProfile();
firefoxprofile.setAssumeUntrustedCertificateIssuer(true);
firefoxprofile.setAcceptUntrustedCertificates(true);
WebDriver driver = new FirefoxDriver(firefoxprofile);
driver.get("https://axisbank.com");
driver.close();
driver.quit();
}
}
From 3.X onwards use FirefoxOptions class
package SSLCertificates;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.CapabilityType;
public class SSLFirefox
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.20.1-win32\\geckodriver.exe");
FirefoxOptions FFOptions = new FirefoxOptions();
FFOptions.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new FirefoxDriver(FFOptions);
driver.get("https://axisbank.com");
driver.close();
driver.quit();
}
}
-------------------------------------------------------------------------------------------------------------------------
Ex2: SSL Certificates for Chrome Driver : Until 2.53.1
--------------------------------------------------------------------------------------
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SSLChrome
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
DesiredCapabilities capability = DesiredCapabilities.chrome();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(capability);
driver.get("https://axisbank.com/");
}
}
From 3.X onwards: Use ChromeOptions
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
public class SSLChrome
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\chromedriver_win321\\chromedriver.exe");
System.setProperty("webdriver.chrome.driver", file.getAbsolutePath());
ChromeOptions capability = new ChromeOptions();
capability.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new ChromeDriver(capability);
driver.get("https://axisbank.com/");
}
}
-------------------------------------------------------------------------------------------------------------------------
Ex2: SSL Certificates for IE Driver : Below example until 2.53.1
--------------------------------------------------------------------------------------
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class SSLForIE
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\IEDriverServer_x64_2.53.1\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
DesiredCapabilities caps = DesiredCapabilities.internetExplorer();
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.get("https://axisbank.com");
}
}
From 3.0 onwards : InternetExplorerOptions();
package SSLCertificates;
import java.io.File;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.ie.InternetExplorerOptions;
import org.openqa.selenium.remote.CapabilityType;
public class SSLForIE
{
public static void main(String[] args)
{
File file = new File("F:\\D-Drive\\FromC\\IEDriverServer_x64_3.12.0\\IEDriverServer.exe");
System.setProperty("webdriver.ie.driver", file.getAbsolutePath());
InternetExplorerOptions caps =new InternetExplorerOptions();
//It will avoid security domain warning message
caps.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS,true);
//It will accept ssl certificate
caps.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
WebDriver driver = new InternetExplorerDriver(caps);
driver.get("https://axisbank.com");
}
}
-------------------------------------------------------------------------------------------------------------------------
Ex4: Proxy server connection
--------------------------------------------------------------------------------------
package SSLCertificates;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.Proxy;
public class ProxyForDriver
{
public static void main(String[] args)
{
String PROXY = "192.168.1.11:8080";
Proxy proxy = new Proxy();
proxy.setHttpProxy(PROXY)
.setFtpProxy(PROXY)
.setSslProxy(PROXY);
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.PROXY, proxy);
WebDriver driver = new FirefoxDriver(cap);
}
}
==================================================================================
How To Download files from firefox Driver: Until 2.53.1
=======================================================
package Examples;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
public class DownLoad
{
public static void main(String[] args) throws Exception
{
/*
1. Create a PROFILE object of Browser.
2. Set Preference, by giving Download destination Directory.
3. Set Preference, by giving Default Folder.
0 => Desktop,
1=>System Default Location,
2 => Indicates a custom Folder Location
4. Set Preference, A comma-separated list of MIME types to save to disk without
asking what to use to open the file. Default value is an empty string.
*/
FirefoxProfile Prof = new FirefoxProfile();
Prof.setPreference("browser.download.dir", "C:\\Down");
Prof.setPreference("browser.download.folderList", 2);
Prof.setPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
WebDriver driver = new FirefoxDriver(Prof);
driver.manage().window().maximize();
driver.get("http://docs.seleniumhq.org/download/");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='mainContent']/p[7]/a[1]")).click();
}
}
From 3.X Onwards
===============================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.firefox.FirefoxProfile;
public class DownLoad
{
public static void main(String[] args) throws Exception
{
/*
1. Create a FirefoxOptions object of Browser.
2. Set Preference, by giving Download destination Directory(Folder).
3. Set Preference, by giving Default Folder.
0 => Desktop,
1=>System Default Location,Means download folder
2 => Indicates a custom Folder Location
4. Set Preference, A comma-separated list of MIME types to save to disk without
asking what to use to open the file. Default value is an empty string.
*/
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.20.1-win32\\geckodriver.exe");
FirefoxOptions Prof = new FirefoxOptions();
Prof.addPreference("browser.download.dir", "C:\\Down");
Prof.addPreference("browser.download.folderList", 2);
Prof.addPreference("browser.helperApps.neverAsk.saveToDisk","application/zip");
//--------------------------------------------------------------------
WebDriver driver = new FirefoxDriver(Prof);
//----------------------------------------------------------------------
driver.manage().window().maximize();
driver.get("http://docs.seleniumhq.org/download/");
Thread.sleep(3000);
driver.findElement(By.xpath("//*[@id='mainContent']/p[7]/a[1]")).click();
}
}
=============================================================
Navigate Commands Example
===============================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateCommands
{
public static void main(String[] args)throws Exception
{
FirefoxDriver Brow=new FirefoxDriver();
Brow.manage().window().maximize();
//---------------------------------------------
Brow.navigate().to("http://www.calculator.net/");
Thread.sleep(3000);
//Verify Home Page
if(Brow.getTitle().equals("Calculator.net: Free Online Calculators - Math, Health, Financial, Science"))
{
System.out.println("Calculator page displayed");
}
//click on All Calculators
Brow.findElement(By.xpath("//*[@id='hcalc']/table/tbody/tr/td[2]/p/a/b")).click();
Thread.sleep(1000);
//click on Due date
Brow.findElement(By.linkText("Due Date Calculator")).click();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
//Go back to previous page
Brow.navigate().back();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Sitemap"))
{
System.out.println("Sitemap page displayed");
}
//Go to next page
Brow.navigate().forward();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
Brow.close();
Brow.quit();
}
}
============================================================
How to use JavaScript Executor:
===========================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class JavScriptExecEx
{
/*
JAVA SCRIPT example
We can use java script command to perform actions.
We can write code to fill up the text box through java script.
Please follow the steps mentioned below.
1. Create Driver for any Browser(Mozilla).
2. Go to the URL.
3. Create Java Script executor object for the Driver.
4. Store the Java Script command in a String Variable.
5. Java Script Executor object executes the command in the Variable.
*/
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://classroom:90/qahrm");
JavascriptExecutor js = (JavascriptExecutor) driver;
String jsUN = "document.getElementsByName('txtUserName')[0].value='qaplanet1'";
js.executeScript(jsUN);
String jsPWD = "document.getElementsByName('txtPassword')[0].value='user1'";
js.executeScript(jsPWD);
driver.findElement(By.cssSelector("input.button")).click();
}
}
=============================================================================
How To find Particular link
=============================================================================
package Examples;
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.firefox.FirefoxDriver;
public class GetAllLinksName
{
public static void main(String[] args)throws Exception
{
WebDriver driver=new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(5,TimeUnit.SECONDS);
driver.get("http://www.calculator.net/");
driver.manage().window().maximize();
List<WebElement> list=driver.findElements(By.tagName("a"));
//System.out.println(list.size());
//int txt=list.size();
//System.out.println(txt);
System.out.println(list.size());
for(int i=0;i<list.size();i++)
{
String txt=list.get(i).getText();
System.out.println(txt);
if(txt.equals("Due Date Calculator"))
{
list.get(i).click();
break;
}
}
driver.close();
driver.quit();
}
}
=================================================================================
Auto Suggestion
================================================================================
import java.util.List;
import java.util.Iterator;
import java.util.concurrent.TimeUnit;
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.firefox.FirefoxDriver;
public class Autosugeestion
{
public static void main(String[] args)throws Exception
{
// System.setProperty("webdriver.chrome.driver", "F:\\D-Drive\\FromC\\chromedriver_win32\\chromedriver.exe");
// WebDriver driver =new ChromeDriver();
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("http://google.com");
WebElement E=driver.findElement(By.id("lst-ib"));
E.sendKeys("selenium");
//find UL with class property
WebElement ul = driver.findElement(By.className("sbsb_b"));
List<WebElement> Lic = ul.findElements(By.tagName("li"));
//-----------------------------------------------------------
//Using Iterator object
//------------------------------------------------------------------------
Iterator<WebElement> i = Lic.iterator();
System.out.println("-----------------------------------------");
while(i.hasNext())
{
Thread.sleep(1000);
WebElement LiI = i.next();
//WebElement E=LiI.findElement(By.xpath("//div[@id='sbse0']/div[2]"));
String data=LiI.findElement(By.cssSelector("div.sbqs_c")).getText();
System.out.println(data);
System.out.println("-----------------------------------------");
if(data.equals("selenium webdriver"))
{
LiI.click();
break;
}
}
//----------------------------------------------------------------
//Using for each
//--------------------------------------------------------------------
for (WebElement LI : Lic)
{
String itemname=LI.findElement(By.cssSelector("div.sbqs_c")).getText();
System.out.println(itemname);
if(itemname.equals("selenium webdriver"))
{
LI.click();
break;
}
}
Thread.sleep(1000);
}
}
=================================================================================
Navigate Commands Example
==================================================================================
package Examples;
import org.openqa.selenium.By;
import org.openqa.selenium.firefox.FirefoxDriver;
public class NavigateCommands
{
public static void main(String[] args)throws Exception
{
FirefoxDriver Brow=new FirefoxDriver();
Brow.manage().window().maximize();
//---------------------------------------------
Brow.navigate().to("http://www.calculator.net/");
Thread.sleep(3000);
//Verify Home Page
if(Brow.getTitle().equals("Calculator.net: Free Online Calculators - Math, Health, Financial, Science"))
{
System.out.println("Calculator page displayed");
}
//click on All Calculators
Brow.findElement(By.xpath("//*[@id='hcalc']/table/tbody/tr/td[2]/p/a/b")).click();
Thread.sleep(1000);
//click on Due date
Brow.findElement(By.linkText("Due Date Calculator")).click();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
//Go back to previous page
Brow.navigate().back();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Sitemap"))
{
System.out.println("Sitemap page displayed");
}
//Go to next page
Brow.navigate().forward();
Thread.sleep(1000);
//Verify Home Page
if(Brow.getTitle().equals("Due Date Calculator"))
{
System.out.println("Due Date Calculator page displayed");
}
Brow.close();
Brow.quit();
}
}
=================================================================================
How To Get Tool Tip
=================================================================================
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class ToolTipEx
{
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver Brow = new FirefoxDriver();
Brow.manage().window().maximize();
Brow.manage().timeouts().pageLoadTimeout(20, TimeUnit.SECONDS);
//------------------------------------------------------------------
Brow.get("http://www.google.co.in/");
//Get Title attribute of Edit box
String ToolTip=Brow.findElement(By.id("lst-ib")).getAttribute("title");
System.out.println(ToolTip);
//close browser
Brow.close();
//quit object
Brow.quit();
}
}
====================================================================================
==================================================================================
How To take screenshot
=====================================================================================
import java.io.File;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.apache.commons.io.FileUtils;
public class Screenshot
{
public static void main(String[] args)throws Exception
{
System.setProperty("webdriver.gecko.driver", "F:\\D-Drive\\FromC\\geckodriver-v0.11.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("http://classroom:90/qahrm");
File scrFile = ((TakesScreenshot)driver).
getScreenshotAs(OutputType.FILE);
// Now copy somewhere
FileUtils.copyFile(scrFile, new File("c:\\screenshot.png"));
driver.close();
driver.quit();
}
}
===========================================================================
Working With Multiple Browsers
==========================================================================
import java.util.Iterator;
import java.util.Set;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
public class ToWorWithMultipleBrowsersk
{
public static void main(String[] args)throws Exception
{
System.setProperty("webdriver.chrome.driver", "F:\\D-Drive\\FromC\\chromedriver_win321\\chromedriver.exe");
ChromeDriver Driver = new ChromeDriver();
Driver.manage().window().maximize();
//-----------------------------------------------
Driver.get("http://qaplanet.in/");
Thread.sleep(2000);
//Click on Linked icon
Driver.findElement(By.xpath("//*[@id='section-tophat']/div[1]/div/div[2]/ul/li[4]/a/img")).click();
Thread.sleep(4000);
Set<String> IDS=Driver.getWindowHandles();
//Way 1
for(String ID : IDS)
{
System.out.println(ID);
}
System.out.println("----------------------------------------");
//Way 2
Iterator<String> ITR=IDS.iterator();
String First_ID=ITR.next();
String Second_ID=ITR.next();
//Switch to second browser
Driver.switchTo().window(Second_ID);
Thread.sleep(4000);
//click on Sign in
Driver.findElement(By.linkText("Sign in")).click();
Thread.sleep(2000);
//Enter email
Driver.findElement(By.name("session_key")).sendKeys("qaplanet@gmail.com");
Thread.sleep(2000);
//close browser
Driver.close();
Thread.sleep(2000);
//--------------------------------------------------------------
//Switch to parent browser
Driver.switchTo().window(First_ID);
//Execute code
Thread.sleep(2000);
//Click on justdial icon
Driver.findElement(By.xpath("//*[@id='section-tophat']/div[1]/div/div[2]/ul/li[5]/a/img")).click();
//Execute code
Thread.sleep(3000);
//---------------------------------------------
Driver.close();
Driver.quit();
}
}
No comments:
Post a Comment