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");
}
}
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");
}
}