Selenium Webdriver Login Script

Selenium Webdriver login script

package selenium_examples;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import java.util.concurrent.TimeUnit;

public class example1 {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://newtours.demoaut.com");
driver.findElement(By.name("userName")).sendKeys("mercury");
driver.findElement(By.name("password")).sendKeys("mercury");
driver.findElement(By.name("login")).click();
driver.manage().timeouts().implicitlyWait(60,TimeUnit.SECONDS);
driver.findElement(By.name("findFlights")).click();
System.out.println("Done!");

}
    }

Above script Opens mercury tours website and logins into the site with given credentials.

and clicks on continue button(with the default values) in Find a Flight  page.


Implicit wait is used for waiting.




How to check and uncheck checkbox with selenium webdriver

Selenium Webdriver checkbox checked n unchecked script

package selenium_examples;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class spicejet_example {

public static void main(String[] args) {

WebDriver driver = new FirefoxDriver();

   driver.get("https://book.spicejet.com/Login.aspx");

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

   WebElement we = driver.findElement(By.id("AvailabilitySearchInputLoginView_Defense"));

   we.click();

   System.out.println(we.isSelected());

   we.click();

   System.out.println(we.isSelected());


}
}

Output of the above script:-

true
false


Above script checks Checkbox,Indian armed forces personnel in https://book.spicejet.com/ website and unchecks the same checkbox.


isSelected() method verifies whether the element is selected or not.Used for checkboxes, options in a select and radio buttons.
It returns true if the element is currently selected or checked, false otherwise.



How to Maximize a firefox browser window using selenium webdriver

How to Maximize a firefox browser window using selenium webdriver?

Maximizing Browser window using Selenium webdriver script

package selenium_examples;


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

public class Test {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://google.com");
driver.manage().window().maximize();
                driver.close();
}

}

Above script maximizes browser window and closes the browser.

First Selenium Webdriver Script


Script 1-

package selenium_examples;

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

public class example1 {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://www.funandknowledge.blogspot.com");
System.out.println(driver.getTitle());
System.out.println(driver.getCurrentUrl());
driver.close();
}

    }


Output will be :-

QTP Scripts
http://www.funandknowledge.blogspot.in/


Webdriver methods used in above script:

driver.get - Takes you to the Website www.funandknowledge.blogspot.com
driver.getTitle - Returns The title of the current page
driver.getCurrentUrl - Returns The URL of the page currently loaded in the browser
driver.close - Closes the current window

Other webdriver methods mostly used:

driver.getPageSource - Returns the page source of the current page
driver.quit - quits driver and closes every associated window


Script 2-

package selenium_examples;


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

public class Sample {

public static void main(String[] args)
   {

WebDriver driver = new FirefoxDriver();
driver.get("http://www.google.com");
driver.findElement(By.id("gbqfq")).sendKeys("site:funandknowledge.blogspot.com");
driver.findElement(By.id("gbqfb")).click();
}

    }

Related Posts Plugin for WordPress, Blogger...

Fun and Knowledge