Skip to content

Instantly share code, notes, and snippets.

@lotosotol
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lotosotol/4bd4a7016304c38b19ec to your computer and use it in GitHub Desktop.
Save lotosotol/4bd4a7016304c38b19ec to your computer and use it in GitHub Desktop.
Lecture61
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class html_unit_test {
public static void main(String[] args) throws InterruptedException {
WebDriver driver = new HtmlUnitDriver(); // this is used for UNIT TESTS !!!!!!!
driver.navigate().to("https://accounts.google.com/");
System.out.println("---------------------------------------------------------------------------------------------------");
System.out.println("------------------------------------- 1. Getting page's title -------------------------------------");
//printing page's title:
System.out.println("Page's title is: '" + driver.getTitle() + "'");
System.out.println("---------------------------------------------------------------------------------------------------");
System.out.println("---------------------------------------------------------------------------------------------------");
System.out.println("---------------------------------- 2. Writing Email and Password ----------------------------------");
//there's an implicit 3 second wait if no elements are found which can be switched on/off:
driver.manage().timeouts().implicitlyWait(0, TimeUnit.MILLISECONDS);
boolean exists = driver.findElements(By.xpath(".//input[@id='Passwd']")).size()!=0;
System.out.println("Check password field exists = " + exists + "\n");
//if "Passwd" field does not exists enter Email and press ENTER:
if (exists == false) {
driver.findElement(By.xpath(".//input[@id='Email']")).sendKeys("seleniumcoaching@gmail.com");
//driver.findElement(By.xpath(".//input[@id='Email']")).sendKeys(Keys.ENTER);
//Thread.sleep(2000L);
System.out.println("---> Email entered...");
//checking again if the "Password" field exists (should return TRUE):
exists = driver.findElements(By.xpath(".//input[@id='Passwd']")).size()!=0;
System.out.println("Re-Check password field exists = " + exists + "\n");
}
//if "Passwd" field exists add Email and Password and press ENTER:
if (exists == true) {
driver.findElement(By.xpath(".//input[@id='Email']")).sendKeys("seleniumcoaching@gmail.com");
System.out.println("---> Email entered...");
driver.findElement(By.xpath(".//input[@id='Passwd']")).sendKeys("abcd");
driver.findElement(By.xpath(".//input[@id='Passwd']")).sendKeys(Keys.ENTER);
System.out.println("---> Password entered...");
}
else {
System.out.println("There is no password field.");
}
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
System.out.println("---------------------------------------------------------------------------------------------------");
System.out.println("DONE!");
}
}
Jun 02, 2015 9:06:26 AM com.gargoylesoftware.htmlunit.html.InputElementFactory createElementNS
INFO: Bad input type: "email", creating a text input
-------------------------------------------------------------------------------------------------
------------------------------------ 1. Getting page's title ------------------------------------
Page's title is: 'Sign in - Google Accounts'
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------
--------------------------------- 2. Writing Email and Password ---------------------------------
Check password field exists = true
---> Email entered...
Jun 02, 2015 9:06:27 AM com.gargoylesoftware.htmlunit.html.InputElementFactory createElementNS
INFO: Bad input type: "email", creating a text input
---> Password entered...
-------------------------------------------------------------------------------------------------
DONE!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment