Selenium With Java and Python For Mobile Apps & Web Apps......!

Saturday, 31 January 2015

Basic Program for Webdriver Beginners-3

Basic Example...

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

public class BasicOpExam {
public static void main(String[] args) {
WebDriver driver=new FirefoxDriver();
driver.get("http://127.0.0.1/login.do");

System.out.println("current url"+driver.getCurrentUrl());

System.out.println("title of page"+driver.getTitle());

System.out.println("page source code"+driver.getPageSource());

System.out.println("class name"+driver.getClass());

System.out.println("window id"+driver.getWindowHandle());

driver.navigate().back();

driver.navigate().forward();

driver.navigate().refresh();

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

System.out.println("done");

driver.quit();
}

}
WebDriver Wait Example:

package basic;

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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class WaitStatementExample {
public static void main(String[] args) throws InterruptedException {
WebDriver driver=new FirefoxDriver();//launch browser
driver.get("http://piyush-pc/login.do");//navigate to actiTime login page
driver.findElement(By.name("username")).sendKeys("admin");
driver.findElement(By.name("pwd")).sendKeys("manager");

Thread.sleep(3000);//using java api wait statement
driver.findElement(By.xpath("//input[@type='submit']")).click();
System.out.println("login done");

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);  //implicit wait statement used wait till entire page get download 
driver.findElement(By.linkText("Projects & Customers")).click();

WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.elementToBeClickable(By.linkText("add project")));

//explicit wait statement used to load web element not entire page used in dynamic app(ajax app)
System.out.println("done");

}


}



Wednesday, 14 January 2015

How to Launch Multiple Browser in Webdriver

Launch Multiple Browser Example:

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.safari.SafariDriver;

public class MutlipleBrowserExample {
public static void main(String[] args) {


String browserType="FireFox";
WebDriver driver=null;
if(browserType.equals("FireFox"))

{

driver=new FirefoxDriver();

}
else if (browserType.equals("Chrome")) {
System.setProperty("webdriver.chrome.driver","E:\\SOFTWARES\\chromedriver.

exe");// path of chrome server for Webdriver
WebDriver d=new ChromeDriver();


}
else if (browserType.equals("IE")) {
System.setProperty("webdriver.ie.driver","C:\\Users\\IEDriverServer.exe");

WebDriver driver = new InternetExplorerDriver();
capabilities.setBrowserName("safari");
CommandExecutor executor = new SeleneseCommandExecutor(new 
URL("http://localhost:4444/"), new URL("http://www.google.com/"), capabilities);
WebDriver driver = new RemoteWebDriver(executor, capabilities);
}
DesiredCapabilities capabilities = new DesiredCapabilities();

}
}

}


Tuesday, 13 January 2015

Basic Program for Webdriver Beginners-1

To Get Current  PageURL:

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

public class CurrentURL {
public static void main(String[] args) {


WebDriver driver=new FirefoxDriver(); 
driver.get("http://advanceseleniumhelp.blogspot.in/");
     Or 
               driver.navigate().to("http://advanceseleniumhelp.blogspot.in/");
   
String s1=driver.getCurrentUrl();
System.out.println(s1);

}
}
To Get Current  Page Title and Page Source(HTML Code):
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PageTitle{
public static void main(String[] args) {


WebDriver driver=new FirefoxDriver(); 
 driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1"); 

 String s1=driver.getTitle(); System.out.println("Page Title is:"+s1);

String s2=driver.getPageSource();
System.out.println("Page Source code is"+s2);

}
}
Page Refresh and Back Page Navigation:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class PageRefresh {
public static void main(String[] args) throws InterruptedException {


WebDriver driver=new FirefoxDriver();
driver.get("http://advanceseleniumhelp.blogspot.in/");
   driver.navigate().refresh();
   driver.findElement(By.linkText("Brief About Test Automation.")).click();
   Thread.sleep(3000);
   driver.navigate().back();



}

}

Sunday, 11 January 2015

How to launch Diffrent-2 Browsers using Selenium WebDriver?

For Mozilla Firefox:

WebDriver driver=new FireFoxdriver();


Chrome:

System.setProperty("webdriver.chrome.driver","E:\\SOFTWARES\\chromedriver.

exe");// path of chrome server for Webdriver
WebDriver d=new ChromeDriver();


Internet Explorer:
Step 1: Please include below code in a class, code should be before creating an

object

System.setProperty("webdriver.ie.driver","C:\\Users\\IEDriverServer.exe");

WebDriver driver = new InternetExplorerDriver();

Dwonload IEDriverServer.exe from the below link and store it in your local

machine

(http://code.google.com/p/selenium/downloads/detail?name=IEDriverServer_x64_

2.29.0.zip&can=2&q)

IE need some security settings :

step 2 : (still if u are facing the problems , do the seetings below)

Open up your Internet Explorer settings tools-->Internet Option --> and have a

look at the Security tab. Is the “Enable Protected Mode” checkbox set to the same

value for all the zones



Safari:

DesiredCapabilities capabilities = new DesiredCapabilities();

capabilities.setBrowserName("safari");

CommandExecutor executor = new SeleneseCommandExecutor(new

URL("http://localhost:4444/"), new URL("http://www.google.com/"), capabilities);

WebDriver driver = new RemoteWebDriver(executor, capabilities);

Translate

Popular Posts

Total Pageviews