Wednesday, 30 August 2017

Code Example - Set Page Dimension

package com.practiceCode;

import org.openqa.selenium.Dimension;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 30-08-2017
 *
 */

public class SetPageDimension extends SuperTestNG {

@Test
public void testDimension() {
driver.manage().window().setSize(new Dimension(1366, 768));
driver.get("http://www.only-testing-blog.blogspot.in/2013/09/testing.html");

}

}

Code Example - Delete Cookies

package com.practiceCode;

import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 30-08-2017
 *
 */

public class DeleteCookies extends SuperTestNG {

@Test
public void testDeleteCookies() {
try {
driver.get("https://www.google.co.in/");
System.out.println("Before cookies delete: " + driver.manage().getCookies());
driver.manage().deleteAllCookies(); //
System.out.println("After cookies delete: " + driver.manage().getCookies());
} catch (Exception e) {
e.printStackTrace();
}
}
}

Tuesday, 22 August 2017

Code Example - Check Box Is Checked by List

package com.practiceCode;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
* @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 23-08-2017
*
*/

public class VerifySelectTheCheckBoxFromList extends SuperTestNG {

@Test
public void testSelectTheCheckBoxFromList() throws InterruptedException {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
List<WebElement> allOptions = driver.findElements(By.tagName("input"));
for (WebElement option : allOptions) {
if ("Boat".equals(option.getAttribute("value"))) {
if (!option.isSelected()) {
option.click();
System.out.println(option.getAttribute("value") + " - checkbox is checked");
break;
} else {
System.out.println(option.getAttribute("value") + " - checkbox is already checked");
}
}
}
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}

}

}

Code Example - Verify Check Box Is Deselect

package com.practiceCode;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
* @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 23-08-2017
*
*/

public class VerifyCheckBoxIsDeselect extends SuperTestNG {

@Test
public void testCheckBoxIsDeselect() throws InterruptedException {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");

WebElement checkBox = driver.findElement(By.name("vehicle"));
checkBox.click();
Thread.sleep(2000);

if (checkBox.isSelected()) {
// De-select the checkbox
checkBox.click();
System.out.println(checkBox.getAttribute("value") + " check box is now deselected");
Thread.sleep(2000);
} else {
System.out.println(checkBox.getAttribute("value") + " check box is not selected");
}
}

}

Code Example - Verify Check Box Is Selected

package com.practiceCode;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
* @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 23-08-2017
*
*/

public class VerifyCheckBoxSelected extends SuperTestNG {

@Test
public void testCheckBoxIsSelected() {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");

WebElement checkBox = driver.findElement(By.name("vehicle"));
if (checkBox.isSelected()) {
System.out.println(checkBox.getAttribute("value") + " checkbox is already selected.");
} else {
// Select the checkbox
checkBox.click();
System.out.println(checkBox.getAttribute("value") + " checkbox is selected.");
}
}

}

Code Example - File Upload By Sendkeys

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class FileUploadBySendkeys extends SuperTestNG {

/**
  * @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 23-08-2017
*
*/

@Test
public void testFileUploadBySendkeys() {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
driver.findElement(By.name("img")).sendKeys("D:\\FileDownload\\Testing Text.txt");
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}

}

Code Example - Print Drop Down Options

package com.practiceCode;

import java.util.Iterator;
import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class GetDropDownOptions extends SuperTestNG {

/**
  * @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 22-08-2017
*
*/

@BeforeClass
public void enterURL() {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
}

@Test()
public void printDropDownOptions() {
try {
WebElement ele = driver.findElement(By.name("FromLB"));
Select select = new Select(ele);
List<WebElement> options = select.getOptions();

/**
* Option 1- for loop
*/

System.out.println("Number of Options:" + options.size());
System.out.println("@@@@@@@ for loop @@@@@@@ ");
for (int i = 0; i < options.size(); i++) {
System.out.println(options.get(i).getText());
}

/**
* Option 2- for each loop
*/
System.out.println("@@@@@@@ for each loop @@@@@@@ ");
for (WebElement a : options) {
System.out.println(a.getText());
}

/**
* Option 3- Iterator loop
*/

System.out.println("@@@@@@@ Iterator loop @@@@@@@ ");
Iterator<WebElement> it = options.iterator();
while (it.hasNext()) {
System.out.println(it.next().getText());
}

} catch (Exception e) {
e.printStackTrace();
}

}

}

Code Example - Drop Down Select

package com.practiceCode;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 22-08-2017
 *
 */

public class SelectSingleDropDown extends SuperTestNG {

String xpathValue = ".//*[@id='post-body-7114928646577071695']/div[1]/div[1]/select";

@BeforeClass
public void enterURL() {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
}

@Test(priority = 0, description = "1 - Method - SelectByVisibleText()")
public void testSelectByVisibleText() throws InterruptedException {

WebElement element = driver.findElement(By.xpath(xpathValue));
Select dropDown = new Select(element);
dropDown.selectByVisibleText("Audi");

WebElement option = dropDown.getFirstSelectedOption();
System.out.println("SelectByVisibleText() Selected value is: " + option.getText());

}

@Test(priority = 1, description = "2 - Method - SelectByValue()")
public void testSelectByValue() throws InterruptedException {
Thread.sleep(1000);
WebElement element = driver.findElement(By.xpath(xpathValue));
Select dropDown = new Select(element);
dropDown.selectByValue("UK");

WebElement option = dropDown.getFirstSelectedOption();
System.out.println("SelectByValue() Selected value is: " + option.getText());

}

@Test(priority = 2, description = "3 - Method - SelectByIndex()")
public void testSelectByIndex() throws InterruptedException {
Thread.sleep(1000);
WebElement element = driver.findElement(By.xpath(xpathValue));
Select dropDown = new Select(element);
dropDown.selectByIndex(2);

WebElement option = dropDown.getFirstSelectedOption();
System.out.println("SelectByIndex() Selected value is: " + option.getText());

}

}

Thursday, 10 August 2017

CrossBrowserTesting.xml

Create one xml file with name as CrossBrowserTesting.xml then paste the code.After save the file run the CrossBrowserTesting.xml file one by one test script run in all browsers.


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="TestSuite" thread-count="3" parallel="none">

<test name="ChromeTest">
<parameter name="browser" value="Chrome" />
<classes>
<class name="com.practiceCode.CrossBrowserScript" />
</classes>
</test>

<test name="FirefoxTest">

<parameter name="browser" value="Firefox" />
<classes>
<class name="com.practiceCode.CrossBrowserScript" />
</classes>
</test>

<test name="IE">
<parameter name="browser" value="IE" />
<classes>
<class name="com.practiceCode.CrossBrowserScript" />
</classes>
</test>

</suite>

Code Example - Cross Browser Test Script


package com.practiceCode;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;


/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 10-08-2017
 *
 */

public class CrossBrowserScript {

WebDriver driver;
private static String[] links = null;
private static int linksCount = 0;

@BeforeTest
@Parameters("browser")
public void setup(String browser) throws Exception {

if (browser.equalsIgnoreCase("firefox")) {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
DesiredCapabilities dc = DesiredCapabilities.firefox();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
FirefoxProfile profile = new FirefoxProfile();
profile.setAcceptUntrustedCertificates(true);
dc.setCapability(FirefoxDriver.PROFILE, profile);
driver = new FirefoxDriver(dc);
}

else if (browser.equalsIgnoreCase("chrome")) {
DesiredCapabilities handlSSLErr = DesiredCapabilities.chrome();
handlSSLErr.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/drivers/chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);
driver = new ChromeDriver(options);

} else if (browser.equalsIgnoreCase("IE")) {
System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + "/drivers/IEDriverServer.exe");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new InternetExplorerDriver(dc);
}

}

@Test
public void testClickOnLinks() throws InterruptedException {

driver.get("https://seleniumcodeexample.blogspot.in/");
List<WebElement> linksize = driver.findElements(By.tagName("a"));
linksCount = linksize.size();
System.out.println("Total no of links Available: " + linksCount);
links = new String[linksCount];
System.out.println("List of links Available: " + links);
// print all the links from webpage
for (int i = 0; i < linksCount; i++) {
links[i] = linksize.get(i).getAttribute("href");
System.out.println(linksize.get(i).getAttribute("href"));
}
// navigate to each Link on the webpage
for (int i = 0; i < linksCount; i++) {
if (links[i] != null && links[i].contains("https://seleniumcodeexample.blogspot.in"))
driver.navigate().to(links[i]);

}

}

}

Code Example - Click On All Links One By One

package com.handleLog4J;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import com.seleniumWebdriver.SuperTestNG;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 10-08-2017
 *
 */

public class ClickOnAllLinks extends SuperTestNG {
private static String[] links = null;
private static int linksCount = 0;

@Test
public void testClickOnLinks() throws InterruptedException {

driver.get("https://seleniumcodeexample.blogspot.in/");
List<WebElement> linksize = driver.findElements(By.tagName("a"));
linksCount = linksize.size();
System.out.println("Total no of links Available: " + linksCount);
links = new String[linksCount];
System.out.println("List of links Available: " + links);
// print all the links from webpage
for (int i = 0; i < linksCount; i++) {
links[i] = linksize.get(i).getAttribute("href");
System.out.println(linksize.get(i).getAttribute("href"));
}
// navigate to each Link on the webpage
for (int i = 0; i < linksCount; i++) {
if (links[i] != null && links[i].contains("https://seleniumcodeexample.blogspot.in"))
driver.navigate().to(links[i]);

}

}
}

Wednesday, 9 August 2017

Log4j.properties

package com.handleLog4J;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.testng.annotations.Test;

/**
 * @Blog Name : https://seleniumcodeexample.blogspot.in
 * @author Deepak Gupta
 * @Created Date 10-08-2017
 *
 */

public class Log4jExample {

public Logger logger = Logger.getLogger(Log4jExample.class);

@Test
public void testLog4j() {
PropertyConfigurator.configure("log4j.properties");
logger.debug("Sample debug message");
logger.info("Sample info message");
logger.warn("Sample warn message");
logger.error("Sample error message");
logger.fatal("Sample fatal message");
}
}

Code Example - Sub Menu Click

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 10-08-2017
 *
 */

public class SubMenuClick extends SuperTestNG {

 @Test
 public void testSubMenuClick() {

  driver.get("http://executeautomation.com/blog/");
  subMenuClickExample1();
 }

 /**
  * Method 1
  */
 public void subMenuClickExample1() {
  Actions actions = new Actions(driver);
  WebElement mainMenu = driver.findElement(By.xpath("//*[@id='menu-item-1885']/a"));
  actions.moveToElement(mainMenu);

  WebElement subMenu = driver.findElement(By.xpath(".//*[@id='menu-item-1887']/a"));
  actions.moveToElement(subMenu).click().build().perform();
 }

 /**
  * Method 2
  */
 public void subMenuClickExample2() {
  Actions action = new Actions(driver);
  WebElement mainMenu = driver.findElement(By.xpath("//*[@id='menu-item-1885']/a"));
  action.moveToElement(mainMenu).moveToElement(driver.findElement(By.xpath(".//*[@id='menu-item-1887']/a"))).click().build().perform();
 }

}

Code Example - Drag And Drop

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 10-08-2017
 *
 */

public class DragAndDrop extends SuperTestNG {

public void handleFrame() {
WebElement ele = driver.findElement(By.className("demo-frame"));
driver.switchTo().frame(ele);

}

@Test
public void testDragAndDrop() {
try {
driver.get("https://jqueryui.com/droppable/");
handleFrame();
WebElement src = driver.findElement(By.xpath("//*[@id='draggable']/p"));
WebElement tar = driver.findElement(By.id("droppable"));

Actions act = new Actions(driver);
act.dragAndDrop(src, tar).perform();

} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Firefox Browser

Working with Firefox Browser 


package com.practiceCode;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 9-08-2017
 *
 */

public class FirefoxBrowser {

WebDriver driver;

@BeforeTest
public void testFirefoxBrowser() {
try {

System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
driver = new FirefoxDriver();
} catch (Exception e) {
e.printStackTrace();
}
}

@Test
public void testGooglePageTitleInFirefoxBrowser() {
driver.navigate().to("http://www.google.com");
String strPageTitle = driver.getTitle();
System.out.println("Page title: - " + strPageTitle);
Assert.assertTrue(strPageTitle.equalsIgnoreCase("Google"), "Page title doesn't match");
}

@AfterTest
public void tearDown() throws InterruptedException {

driver.quit();

}
}

Code Example - Firefox Set Home Page

package com.practiceCode;

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


/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 9-08-2017
 *
 */

public class FirefoxSetHomePage {

WebDriver driver;

@Test
public void testFireFoxProfile() throws InterruptedException {

System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("browser.startup.homepage", "http://www.google.com");
driver = new FirefoxDriver(profile);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id("lst-ib")).sendKeys("100");

}

@AfterTest
public void end() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}

}

Tuesday, 8 August 2017

Code Example - Take Screen Shot

package com.practiceCode;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 8-08-2017
 *
 */
public class TakeScreenShot extends SuperTestNG {

@Test
public void test() throws InterruptedException, IOException {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
// Capture entire page screenshot and then store it to destination drive
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(screenshot, new File(System.getProperty("user.dir") +      "/ScreenShot/TakeScreenshot.jpg"));
System.out.print("Screenshot is captured and stored in your D: Drive");
}

}

Code Example - Right Click

package com.practiceCode;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 8-08-2017
 *
 */

public class RightClick extends SuperTestNG {

@Test
public void testRightClick() throws InterruptedException {
try {
driver.navigate().to("http://only-testing-blog.blogspot.in/2013/09/testing.html");
Thread.sleep(2000);

WebElement test = driver.findElement(By.id("navbar"));
Actions myAction = new Actions(driver);
myAction.contextClick(test).build().perform();

Thread.sleep(2000);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

Code Example - Navigate Browser

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 8-08-2017
 *
 */

public class NavigateBrowser extends SuperTestNG {

@Test
public void handleNavigateBrowser() {
try {
driver.get("https://jqueryui.com/datepicker/");
System.out.println("Current URL is: " + driver.getCurrentUrl());

driver.findElement(By.linkText("Autocomplete")).click();
Thread.sleep(2000);
System.out.println("After Clicked Link Current URL is: " + driver.getCurrentUrl());
// browser back
driver.navigate().back();
System.out.println("After Navigate Back Current URL is: " + driver.getCurrentUrl());
Thread.sleep(2000);
// browser forward
driver.navigate().forward();
System.out.println("After Navigate Forword Current URL is: " + driver.getCurrentUrl());
Thread.sleep(2000);
// refresh page
driver.navigate().refresh();
Thread.sleep(2000);

} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Mouse Hover

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 8-08-2017
 *
 */
public class MouseHover extends SuperTestNG {

@Test
public void handleMouseHover() throws InterruptedException {

try {
driver.navigate().to("http://only-testing-blog.blogspot.in/2015/03/chart.html");
WebElement element = driver.findElement(By.linkText("Hover over me"));
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();

Thread.sleep(2000);

} catch (Exception e) {
System.out.println("Expection is : " + e);
}
}

}

Code Example - Get Atrribute

package com.practiceCode;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 8-08-2017
 *
 */

public class GetAttribute extends SuperTestNG {

@Test
public void getAttribute() throws InterruptedException {
try {

driver.get("file:///D:/Office/Eclipse_Projects/Project_Neon/SeleniumCodeExampleBlog/WebPage/Practice%20Form.htm");
driver.findElement(By.id("text2")).sendKeys("Gupta");

String value = driver.findElement(By.id("text2")).getAttribute("value");
System.out.println("Entered value is: " + value);

} catch (Exception e) {
e.printStackTrace();
}

}

}

Code Example - Tooltip

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.Assert;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 8-08-2017
 *
 */
public class TooltipExample extends SuperTestNG {

@Test
public void verifyTooltip() throws InterruptedException {

try {
driver.navigate().to("http://only-testing-blog.blogspot.in/2015/03/chart.html");
WebElement element = driver.findElement(By.linkText("Hover over me"));
String toolTipText = element.getAttribute("title");
System.out.println("Tooltip value is: " + toolTipText);
Actions action = new Actions(driver);
action.moveToElement(element).perform();
Assert.assertEquals(toolTipText, "tooltip text!");

} catch (Exception e) {
System.out.println("Expection is : " + e);
}
}

}

Code Example - Test Login Form

package com.seleniumWebdriver;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;


/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 8-08-2017
 *
 */

public class TestLoginForm {

WebDriver driver;

@BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
driver = new FirefoxDriver();
driver.get("https://github.com/login");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

}

@Test
public void verifyInvalidCredentials() throws InterruptedException {

driver.findElement(By.id("login_field")).sendKeys("deepak");
driver.findElement(By.id("password")).sendKeys("testing");
driver.findElement(By.name("commit")).click();

String actual = driver.findElement(By.xpath("//*[@id='js-flash-container']/div")).getText();
System.out.println("Actual Error message is: " + actual);
// Verify the actual and expected error message.
Assert.assertEquals(actual, "Incorrect username or password.");
}

@AfterTest
public void tearDown() {
driver.quit();
}

}

Monday, 7 August 2017

Code Example - iframe With Different Methods

package com.seleniumWebdriver;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.NoSuchFrameException;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 7-08-2017
 *
 */
public class IframeWithDifferentMethods {
static WebDriver driver;

public static boolean isElementPresent(WebElement element) {
boolean flag = false;
try {
if (element.isDisplayed() || element.isEnabled())
flag = true;
} catch (NoSuchElementException e) {
flag = false;
} catch (StaleElementReferenceException e) {
flag = false;
}
return flag;
}

public void switchToFrame(int frame) {
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame with id " + frame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + frame + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with id " + frame + e.getStackTrace());
}
}

public void switchToFrame(String frame) {
try {
driver.switchTo().frame(frame);
System.out.println("Navigated to frame with name " + frame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + frame + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with id " + frame + e.getStackTrace());
}
}

public void switchToFrame(WebElement frameElement) {
try {

if (isElementPresent(frameElement)) {
driver.switchTo().frame(frameElement);
System.out.println("Navigated to frame with element " + frameElement);
} else {
System.out.println("Unable to navigate to frame with element " + frameElement);
}
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with element " + frameElement + e.getStackTrace());
} catch (StaleElementReferenceException e) {
System.out.println("Element with " + frameElement + "is not attached to the page document" + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to frame with element " + frameElement + e.getStackTrace());
}
}

public void switchToFrame(String ParentFrame, String ChildFrame) {
try {
driver.switchTo().frame(ParentFrame).switchTo().frame(ChildFrame);
System.out.println("Navigated to innerframe with id " + ChildFrame + "which is present on frame with id" + ParentFrame);
} catch (NoSuchFrameException e) {
System.out.println("Unable to locate frame with id " + ParentFrame + " or " + ChildFrame + e.getStackTrace());
} catch (Exception e) {
System.out.println("Unable to navigate to innerframe with id " + ChildFrame + "which is present on frame with id" + ParentFrame + e.getStackTrace());
}
}

public void switchtoDefaultFrame() {
try {
driver.switchTo().defaultContent();
System.out.println("Navigated back to webpage from frame");
} catch (Exception e) {
System.out.println("unable to navigate back to main webpage from frame" + e.getStackTrace());
}
}
}

Code Example - IE Untrusted Connection

package com.seleniumWebdriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 7-08-2017
 *
 */
public class IEUntrustedConnection {

WebDriver driver;
String url = "Enter your url";

@Test
public void HandleIEUntrustedConnection() {

System.setProperty("webdriver.ie.driver", "./Browser_Driver/IEDriverServer.exe");
DesiredCapabilities dc = new DesiredCapabilities();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
driver = new InternetExplorerDriver(dc);
driver.get(url);
String js = "javascript:document.getElementById('overridelink').click();";
driver.navigate().to(js);
}

@AfterClass
public void end() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}

}

Code Example - IE Browser

package com.practiceCode;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class IEBrowser {

/**
* @Blog Name : Selenium Code Example
* @author Deepak Gupta
* @Created Date 22-08-2017
*
*/

WebDriver driver;

@BeforeTest
public void openIEBrowser() {

System.setProperty("webdriver.ie.driver", System.getProperty("user.dir") + "/drivers/IEDriverServer.exe");

DesiredCapabilities cap = DesiredCapabilities.internetExplorer();
cap.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);

driver = new InternetExplorerDriver();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}

@Test
public void testGooglePageTitleInIEBrowser() throws InterruptedException {

driver.navigate().to("https://seleniumcodeexample.blogspot.in/");
String strPageTitle = driver.getTitle();
System.out.println("Page title: - " + strPageTitle);
Assert.assertTrue(strPageTitle.equalsIgnoreCase("Selenium WebDriver With Core Java Code Examples For Practice"),
"Page title doesn't match");

}

@AfterTest
public void closeIEBrowser() throws InterruptedException {
driver.quit();
}

}

Code Example - Frames

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 7-08-2017
 *
 */
public class HandlingFrames extends SuperTestNG {

@Test
public void handleFrames() {
try {
driver.get("https://jqueryui.com/tabs/");
WebElement ele = driver.findElement(By.className("demo-frame"));
driver.switchTo().frame(ele);
driver.findElement(By.id("ui-id-3")).click();
logger.info("Tab is clicked");

// Back to default Content
driver.switchTo().defaultContent();
driver.findElement(By.linkText("Demos")).click();
logger.info("Demos link is clicked");
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}

}

Code Example - Get Title Name

package com.seleniumWebdriver;

import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 7-08-2017
 *
 */
public class GetTitleName  {

@BeforeTest
public void preCondtion() {
try {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

} catch (Exception e) {
org.testng.Assert.fail("Test failed");
}

}



@AfterClass
public void end(){
driver.quit();
}

@Test
public void getTitleExample() {
try {

driver.get("http://www.google.com");
System.out.println("Current Title Name: " + driver.getTitle());
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Get Page Source

package com.seleniumWebdriver;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 7-08-2017
 *
 */
public class GetPageSource {

@BeforeTest
public void preCondtion() {
try {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

} catch (Exception e) {
org.testng.Assert.fail("Test failed");
}

}



@AfterClass
public void end(){
driver.quit();
}

@Test
public void handleGetPageSourceExample() {
try {

driver.get("http://www.google.com");
Thread.sleep(2000);
logger.info("page source " + driver.getPageSource());
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Friday, 4 August 2017

Code Example - Get Links

package com.practiceCode;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FindNoOfLinks extends SuperTestNG {

@Test
public void test() throws InterruptedException {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
List<WebElement> no = driver.findElements(By.tagName("a"));
int nooflinks = no.size();
System.out.println("Number of Links : " + nooflinks);
for (WebElement pagelink : no) {
String linktext = pagelink.getText();
String link = pagelink.getAttribute("href");
System.out.println(linktext + " ->");
System.out.println(link);
}
} catch (Exception e) {
System.out.println("error " + e);
}

}

}

Code Example - Get Current URL

package com.seleniumWebdriver;

import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class GetCurrentURL extends SuperTestNG {

@Test
public void handleGetCurrentURLExample() {
try {
driver.get("http://www.google.com");
System.out.println("Current URL: " + driver.getCurrentUrl());
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Get Current Date

package com.practiceCode;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class GetCurrentDate {

@Test
public void testGetCurrentDate() {

try {
// "dd-MMM-YYYY hh:mm a"
DateFormat dateFormat2 = new SimpleDateFormat("dd-MMM-YYYY hh:mm a");
Date date2 = new Date();

String today = dateFormat2.format(date2);
System.out.println("Current date is :  " + today);

} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Get Browser Detail

package com.seleniumWebdriver;

import org.openqa.selenium.Capabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class GetBrowserDetail {

WebDriver driver;

@BeforeTest
public void setup() {

System.setProperty("webdriver.firefox.marionette", System.getProperty("user.dir") + "/drivers/geckodriver");
driver = new FirefoxDriver();

// Check and print Firefox browser and OS detail.
CheckBrowserOS();
driver.close();

System.setProperty("webdriver.chrome.driver", "./Browser_Driver/chromedriver.exe");
driver = new ChromeDriver();
// Check and print Chrome browser and OS detail.
CheckBrowserOS();
driver.close();

System.setProperty("webdriver.ie.driver", "./Browser_Driver/IEDriverServer.exe");
driver = new InternetExplorerDriver();
// Check and print IE browser and OS detail.
CheckBrowserOS();
driver.close();
driver.quit();
}

public void CheckBrowserOS() {
// Get Browser name and version.
Capabilities caps = ((RemoteWebDriver) driver).getCapabilities();
String browserName = caps.getBrowserName();
String browserVersion = caps.getVersion();

// Get OS name.
String os = System.getProperty("os.name").toLowerCase();
System.out.println("\n OS = " + os + ", Browser = " + browserName + " " + browserVersion);
}

@Test
public void testBrowserOS() {
System.out.println("\n==============================================");
}
}

Code Example - Firefox Untrusted Connection

package com.seleniumWebdriver;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FirefoxUntrustedConnection {

public static void main(String[] args) {
FirefoxProfile prof = new FirefoxProfile();
prof.setAssumeUntrustedCertificateIssuer(true);
prof.setAcceptUntrustedCertificates(true);

System.setProperty("webdriver.gecko.driver", "./Browser_Driver/geckodriver.exe");
WebDriver driver = new FirefoxDriver(prof);
driver.get("https://www.google.co.in/");

}

}

Code Example - FireFox Profile

package com.practiceCode;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FireFoxProfileExample {

WebDriver driver;

@BeforeTest
public void setUp() {
System.setProperty("webdriver.gecko.driver", System.getProperty("user.dir") + "/drivers/geckodriver.exe");
// Create object of webdriver's inbuilt class ProfilesIni to access Its method getProfile.
ProfilesIni firProfiles = new ProfilesIni();
// Get access of newly created profile WebDriver_Profile.
FirefoxProfile wbdrverprofile = firProfiles.getProfile("WebDriver_Profile");
// Pass wbdrverprofile parameter to FirefoxDriver.
driver = new FirefoxDriver(wbdrverprofile);
}

@Test
public void OpenURL() {
driver.get("http://only-testing-blog.blogspot.in/2014/05/login.html");
}

@AfterTest
public void tearDown() throws InterruptedException {
Thread.sleep(2000);
driver.quit();
}

}

Code Example - Find Number Of Text Fields

package com.practiceCode;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */

public class FindNoOfTextFields extends SuperTestNG {

@Test
public void testNoOfTextFields() {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
List<WebElement> e = driver.findElements(By.xpath("//input[@type='text']"));
System.out.println("No. of Text Field is: " + e.size());

} catch (Exception e) {
e.printStackTrace();
}
}

}

Code Example - Find Number Of Radio Button

package com.practiceCode;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FindNoOfRadioButton extends SuperTestNG {

@Test
public void testNoOfRadioButton() {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
List<WebElement> e = driver.findElements(By.xpath("//input[@type='radio']"));
System.out.println("No. of Radio Button is: " + e.size());

} catch (Exception e) {
e.printStackTrace();
}
}

}

Code Example - Find Number Of Links

package com.seleniumWebdriver;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FindNoOfLinks extends SuperTestNG {

@Test
public void handleNoofLinks() {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
List<WebElement> e = driver.findElements(By.tagName("a"));
int n = e.size();
logger.info("No. of link is: " + e.size());
for (int i = 0; i < n; i++) {
logger.info(e.get(i).getText());
}
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}

}

Code Example - Find Number Of CheckBox

package com.seleniumWebdriver;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FindNoOfCheckBox extends SuperTestNG {

@Test
public void handleNoOfCheckBox() {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
List<WebElement> e = driver.findElements(By.xpath("//input[@type='checkbox']"));
logger.info("No. of check boxes: " + e.size());
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}

}

Code Example - File Upload By Sendkeys

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FileUploadSendkeys extends SuperTestNG {

@Test
public void handleCalendarPopup() {
try {

driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
driver.findElement(By.xpath(".//*[@id='post-body-7114928646577071695']/div[1]/div[1]/form[1]/input[8]")).sendKeys("D:\\FileDownload\\Testing Text.txt");
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}

}

Code Example - Firefox File Download

package com.seleniumWebdriver;

import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class FileDownloadFirefox {

WebDriver driver;
Logger logger = Logger.getLogger(FileDownloadFirefox.class);

@BeforeTest
public void setUp() {
PropertyConfigurator.configure("Log4j.properties");
System.setProperty("webdriver.gecko.driver", "./Browser_Driver/geckodriver.exe");

// Create object of FirefoxProfile in built class to access Its properties.
FirefoxProfile fprofile = new FirefoxProfile();
// Set Location to store files after downloading.
fprofile.setPreference("browser.download.dir", "D:\\FileDownload\\");
fprofile.setPreference("browser.download.folderList", 2);
// Set Preference to not show file download confirmation dialogue using MIME types Of different file extension types.
fprofile.setPreference("browser.helperApps.neverAsk.saveToDisk",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;"// MIME types Of MS Excel File.
+ "application/pdf;" // MIME types Of PDF File.
+ "application/vnd.openxmlformats-officedocument.wordprocessingml.document;" // MIME types Of MS doc File.
+ "text/plain;"// MIME types Of text File.
+ "application/zip;"// MIME types Of Zip File.
+ "text/csv"); // MIME types Of CSV File.
fprofile.setPreference("browser.download.manager.showWhenStarting", false);
fprofile.setPreference("pdfjs.disabled", true);
// Pass fprofile parameter In webdriver to use preferences to download file.
driver = new FirefoxDriver(fprofile);
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.manage().window().maximize();
}

@AfterTest
public void tearDown() throws InterruptedException {
try {
if (driver != null) {
driver.quit();
}
} catch (Exception e) {
System.out.println("Expection is :" + e);
}
}

@Test(priority = 0)
public void downloadTextFile() {
try {
driver.get("http://only-testing-blog.blogspot.in/2014/05/login.html");

// Download Text File
driver.findElement(By.linkText("Download Text File")).click();
logger.info("Download Text File link is clicked");

Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test(priority = 1)
public void downloadPDFFile() {
try {

// Download PDF File
driver.findElement(By.linkText("Download PDF File")).click();
logger.info("Download PDF File link is clicked");
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test(priority = 2)
public void downloadCSVFile() {
try {

// Download CSV File
driver.findElement(By.linkText("Download CSV File")).click();
logger.info("Download CSV File link is clicked");
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test(priority = 3)
public void downloadExcelFile() {
try {

// Download Excel File
driver.findElement(By.linkText("Download Excel File")).click();
logger.info("Download Excel File link is clicked");
Thread.sleep(2000);
} catch (Exception e) {
e.printStackTrace();
}
}

@Test(priority = 4)
public void downloadDocFile() {
try {

// Download Doc File
driver.findElement(By.linkText("Download Doc File")).click();
logger.info("Download Doc File link is clicked");
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Drag And Drop

package com.seleniumWebdriver;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class DragAndDrop extends SuperTestNG {

public void handleFrame() {
WebElement ele = driver.findElement(By.className("demo-frame"));
driver.switchTo().frame(ele);

}

@Test
public void handleDragAndDrop() {
try {
driver.get("https://jqueryui.com/droppable/");
handleFrame();
WebElement src = driver.findElement(By.xpath("//*[@id='draggable']/p"));
WebElement tar = driver.findElement(By.id("droppable"));

Actions act = new Actions(driver);
act.dragAndDrop(src, tar).perform();
Thread.sleep(3000);

} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Double Click

package com.practiceCode;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class DoubleClick extends SuperTestNG {

@Test
public void testDoubleClick() {
try {
driver.get("http://only-testing-blog.blogspot.in/2013/09/testing.html");
WebElement ele = driver.findElement(By.xpath("//*[text()='Saturday, 21 September 2013']"));
Actions act = new Actions(driver);
act.doubleClick(ele).build().perform();
Thread.sleep(3000);

} catch (Exception e) {
e.printStackTrace();
}
}
}

Code Example - Chrome Browser

package com.seleniumWebdriver;

import java.util.HashMap;
import java.util.Map;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.Test;

/**
 * @Blog Name : Selenium Code Example
 * @author Deepak Gupta
 * @Created Date 4-08-2017
 *
 */
public class ChromeBrowser {

WebDriver driver;

@Test
public void testChromeBrowser() {
try {

// Handle https - Your connection is not private
DesiredCapabilities dc = DesiredCapabilities.chrome();
dc.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);

// Handle Set Chrome driver path and call
System.setProperty("webdriver.chrome.driver", System.getProperty("user.dir") + "/drivers/chromedriver.exe");

// Handle - disable the invalid Extension
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");

// Handle - disable the save password Extension
Map<String, Object> prefs = new HashMap<String, Object>();
prefs.put("credentials_enable_service", false);
prefs.put("profile.password_manager_enabled", false);
options.setExperimentalOption("prefs", prefs);

// Handle - Call the Chrome browser
driver = new ChromeDriver(options);
driver.get("https://www.google.co.in");

} catch (Exception e) {
e.printStackTrace();
}
}

@AfterTest
public void tearDown() throws InterruptedException {

driver.quit();

}
}

Code Example - File Download By Robot Class In Firefox Browser

Exported from Notepad++ package com . practiceCode ; import java . awt . AWTException ; import java . awt . Robot ; import j...