Handling Certifications for Firefox and Chrome Browsers

package testng;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

public class handleCertificate {
	
	WebDriver driver;
	
	@Test
	public void forFirefox()
	{
		DesiredCapabilities	obj = new DesiredCapabilities();
		
		obj.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
		
		driver=new FirefoxDriver(obj);
		
		driver.get("http://www.cacert.org");
		
		driver.quit();
	}
	@Test
	public void forChrome()
	{
		DesiredCapabilities	obj = new DesiredCapabilities();
		
		obj.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
		
		System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
		
		driver=new ChromeDriver(obj);
		
		driver.get("http://www.cacert.org");
		
		driver.quit();
	}

}

 

Select dropdown using Selenium WebDriver

Select specific values from dropdown:

package testng;

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;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class selectDropdown {
	
	WebDriver driver;
	
	@Test
	public void facebookDropdown() throws InterruptedException
	{
		System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
		
		driver = new ChromeDriver();
		
		driver.manage().window().maximize();
		  
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		driver.get("http://www.facebook.com");
		
		WebElement monthDropdown = driver.findElement(By.id("month"));
		
		Select mdd = new Select(monthDropdown);
		
		//Select March
		mdd.selectByIndex(4);
		
		Thread.sleep(3000);
		
		//Select September
		mdd.selectByValue("9");
		
		Thread.sleep(5000);
		
		//Select June
		mdd.selectByVisibleText("Jun");
		
		driver.quit();
	}

}

Verify dropdown values:

package testng;

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;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

public class selectDropdownVerify {
	
	WebDriver driver;
	
	@Test
	public void facebookDropdown() throws InterruptedException
	{
		System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
		
		driver = new ChromeDriver();
		
		driver.manage().window().maximize();
		  
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		driver.get("http://www.facebook.com");
		
		WebElement monthDropdown = driver.findElement(By.id("month"));
		
		Select mdd = new Select(monthDropdown);
		
		WebElement defaultValue = mdd.getFirstSelectedOption();
		
		System.out.println("Default Value: -----"+defaultValue.getText()+"\n"+"\n");
		
		//Select April
		mdd.selectByIndex(4);
		
		WebElement selectedValue = mdd.getFirstSelectedOption();
		
		System.out.println("Selected Value: -----"+selectedValue.getText()+"\n"+"\n");
		
		driver.quit();
	}

}

Print all select dropdown values:

package testng;

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

import org.junit.Assert;
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 org.testng.annotations.Test;

public class selectDropdownTotalValues {
	
	WebDriver driver;
	
	@Test
	public void facebookDropdown() throws InterruptedException
	{
		System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
		
		driver = new ChromeDriver();
		
		driver.manage().window().maximize();
		  
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		driver.get("http://www.facebook.com");
		
		WebElement monthDropdown = driver.findElement(By.id("month"));
		
		Select mdd = new Select(monthDropdown);
		
		List<WebElement>monthOptions = mdd.getOptions();
		
		int totalMonthOptions = monthOptions.size();
		
		Assert.assertEquals(totalMonthOptions, 13);
		
		System.out.println("Total month count: ---"+totalMonthOptions+"\n");
		
		for (WebElement opt:monthOptions)
		{
			String monthName = opt.getText();
			
			System.out.println("Month Name: ---"+monthName+"\n"+"\n");
		}
		
		driver.quit();
	}

}

 

To use configuration file in Selenium Webdriver

package testng;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.Test;

import utility.configReader;

public class googleTestwithConfigFile {
	
	@Test
	public void chromeTest() throws Exception
	{	
		configFile config = new configFile();
		
		System.setProperty("webdriver.chrome.driver", config.getChromePath());
		
		WebDriver driver = new ChromeDriver();
		
		driver.get(config.getApplicationURL());
		
		driver.quit();
	}

}

Configuration File

package utility;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

public class configReader {
	
	Properties pro;
	
	public configFile()
	{
		try {
			File source = new File ("./configuration/configproperties");
			
			FileInputStream input = new FileInputStream(source);
			
			pro = new Properties();
			
			pro.load(input);
			
		} catch (Exception exp) {
			
			System.out.println("Exception is: ---"+exp.getMessage());
		} 
	}
	public String getChromePath()
	{
		String path = pro.getProperty("ChromeDriver");
		
		return path;
	}
	public String getApplicationURL()
	{
		return pro.getProperty("URL");
	}

}

configPropertiesFile

ChromeDriver = E:\\Selenium\\chromedriver.exe

URL=http://www.google.com

 

To take screenshot of failed test case in Selenium Webdriver

package testng;

import java.util.concurrent.TimeUnit;

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

public class gmailScreenshot {
	
	WebDriver driver;
	
	@Test
	public void captureScreenshot() throws InterruptedException
	{
		System.setProperty("webdriver.chrome.driver", "E:\\Selenium\\chromedriver.exe");
		  
		driver=new ChromeDriver();
		  
		driver.manage().window().maximize();
		  
		driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
		
		driver.get("http://www.gmail.com");	
		
		Thread.sleep(2000);			
		
		driver.findElement(By.xpath(".//*[@id='Email']")).click();
		
		driver.findElement(By.xpath(".//*[@id='Email']")).sendKeys("Automation");		
		
		
	}
	
	@AfterMethod
	public void testIT(ITestResult result)
	{
		if(ITestResult.FAILURE==result.getStatus())
		{
			utility.captureScreenshot(driver, result.getName());
		}
		
		driver.quit();
	}
	
	

}
package testng;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

public class utility 
{

	public static void captureScreenshot(WebDriver driver, String screenshotname) 
	{
		try
		{
			TakesScreenshot screenshot =(TakesScreenshot)driver;
			
			File source = screenshot.getScreenshotAs(OutputType.FILE);
			
			FileUtils.copyFile(source, new File("./Screenshots/"+screenshotname+".png"));
			
			System.out.println("Screenshot Taken");
			
		}catch(Exception excep)
		{
			System.out.println("Throwing exception while taking screenshot" +excep.getMessage());
		}

	}

}

 

Selenium 2.0 – Locators, Webelements and RenderedWebElement

Locators
Locator Example (Java)
id attribute By.id(“myElementId”)
name attribute By.name(“myElementName”)
XPATH By.xpath(“//input[@id=’myElementId’]”)
Class name By.className(“even-table-row”)
CSS Selector By.cssSelector(“h1[title]”)
Link Text By.linkText(“Click Me!”)
By.partialLinkText(“ck M”)
Tag Name By.tagName(“td”)
WebElement Methods
Method Purpose
clear() Clears all of the contents if the element is a text entity.
click() Simulates a mouse click on the element.
getAttribute(String name) Returns the value associated with the provided attribute name (if present) or null (if not present).
getTagName() Returns the tag name for this element.
getText() Returns the visible text contained within this element (including subelements) if not hidden via CSS.
getValue() Gets the value of the element’s “value” attribute.
isEnabled() Returns true for input elements that are currently enabled; otherwise false.
isSelected() Returns true if the element (radio buttons, options within a select, and checkboxes) is currently selected; otherwise false.
sendKeys(CharSequence…
keysToSend)
Simulates typing into an element.
setSelected() Select an element (radio buttons, options within a select, and checkboxes).
submit() Submits the same block if the element if the element is a form (or contained within a form). Blocks until new page is loaded.
toggle() Toggles the state of a checkbox element.
Method Purpose
findElement(By by) Finds the first element located by the provided method (see Locators table).
findElements(By by) Finds all elements located by the provided method.
Method Purpose
selectByIndex(int index)/
deselectByIndex(int index)
Selects/deselects the option at the given index.
selectByValue(String value)/
deselectByValue(String value)
Selects/deselects the option(s) that has a value matching the argument.
selectByVisibleText(String text)/
deselectByVisibleTest(String text)
Selects/deselects the option(s) that displays text matching the argument.
deselectAll() Deselects all options.
getAllSelectedOptions() Returns a List<WebElement> of all selected options.
getFirstSelectedOption() Returns a WebElement representing the first selected option.
getOptions() Returns a List<WebElement> of all options.
isMultiple() Returns true if this is a multi-select list; false otherwise.
RenderedWebElement Methods
Method Purpose
dragAndDropBy(int moveRightBy,
int moveDownBy)
Drags and drops the element moveRightBy pixels to the right and moveDownBy pixels down. Pass negative arguments to move left and up.
dragAndDropOn(RenderedWeb
Element element)
Drags and drops the element on the supplied element.
getLocation() Returns a java.awt.Point representing the top left-hand corner of the element.
getSize() Returns a java.awt.Dimension representing the width and height of the element.
getValueOfCssProperty(String
propertyName)
Returns the value of the provided property.
hover() Simulates a mouse hover event over the element.
isDisplayed() Returns true if the element is currently displayed; otherwise false.
Locator Examples
id attribute @FindBy(how = How.ID, using = “myElementId”)
@FindBy(id = “myElementId)
name attribute @FindBy(how = How.NAME, using = “myElementName”)
@FindBy(name = “myElementName”)
id or name
attribute
@FindBy(how = How.ID_OR_NAME, using = “myElement”)
XPATH @FindBy(how = How.XPATH, using = “//input[@id=’myElementId’]”)
@FindBy(xpath = “//input[@id=’myElementId’]”)
Class name @FindBy(how = How.CLASS_NAME, using=”even-table-row”)
@FindBy(className = “even-table-row”)
Link Text @FindBy(how = How.LINK_TEXT, using=”Click Me!”)
@FindBy(linkText = “Click Me!”)
Partial Link Text @FindBy(how = How.PARTIAL_LINK_TEXT, using=”ck M”)
@FindBy(partialLinkText = “ck M”)
Tag Name @FindBy(how = How.TAG_NAME, using=”td”)
@FindBy(tagName = “td”)

Set browser dimension in Selenium Webdriver

package webdriver;

import java.util.concurrent.TimeUnit;

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

public class browserdimension {

public static WebDriver driver;

public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”, “E:\\chromedriver.exe”);

driver=new ChromeDriver();

driver.manage().window().maximize();

driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);

driver.get(“http://facebook.com/”);

System.out.println(driver.manage().window().getSize());

Dimension dim = new Dimension(600,600);

driver.manage().window().setSize(dim);

driver.quit();

}

}

1 2 3 4 5