0% found this document useful (0 votes)
49 views

Untitled

The document contains code to automate login and logout functionality on an application using Selenium WebDriver. It opens the Firefox browser, navigates to the URL, enters username and password in the respective fields, clicks the login button, waits for the logout link to be visible and prints the title of the home page before closing the browser. It also contains questions and answers related to implicit wait, explicit wait, clicking buttons without using click(), changing and clearing textbox values, retrieving tooltip text, coordinates and dimensions of elements.

Uploaded by

prabhakar r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Untitled

The document contains code to automate login and logout functionality on an application using Selenium WebDriver. It opens the Firefox browser, navigates to the URL, enters username and password in the respective fields, clicks the login button, waits for the logout link to be visible and prints the title of the home page before closing the browser. It also contains questions and answers related to implicit wait, explicit wait, clicking buttons without using click(), changing and clearing textbox values, retrieving tooltip text, coordinates and dimensions of elements.

Uploaded by

prabhakar r
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

public class Demo010216 {

public static void main(String[] args)


{
//script to login to actitime
//open the browser
WebDriver driver=new FirefoxDriver();
//enter the url
driver.get("http://localhost/login.do");
//enter the username
driver.findElement(By.id("username")).sendKeys("admin");
//enter the password
driver.findElement(By.name("pwd")).sendKeys("manager");
//click on login button
driver.findElement(By.xpath("//div[text()='Login ']")).click();
//wait till logout link is visible within 30Sec
WebDriverWait wait=new WebDriverWait(driver,30);
wait.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.id("logoutLink")));
//get the title of home page and print it
String title=driver.getTitle();
System.out.println(title);

IQ7) What are the differences between ImplicitWait and ExplicitWait

ImplicitWait ExplicitWait
1) We do not specify any condition We should specify the condition
2) We can only handle findElement() and We can handle any method
findElements()
3) After the timeout we get After the timeout we get TimeOutException
NoSuchElementException
4) Time unit can be DAYS,HOURS,SECONDS etc It can be only SECONDS

IQ8) Write a script to login and logout from the application without specifying any type of waiting
duration(period)

Answer: 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 Demo010216 {

26
public static void main(String[] args)
{
//script to login to actitime
//open the browser
WebDriver driver=new FirefoxDriver();
//enter the url
driver.get("http://localhost/login.do");
//enter the username
driver.findElement(By.id("username")).sendKeys("admin");
//enter the password
driver.findElement(By.name("pwd")).sendKeys("manager");
//click on login button
driver.findElement(By.xpath("//div[text()='Login ']")).click();
while(true)
{
try
{
driver.findElement(By.id("logoutLink")).click();
break;
}
catch(Exception e)
{
}
}
}

}
IQ9) How do you click on a button without using click()

Answer: By pressing enter key

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

public class Demo01216 {

public static void main(String[] args)


{
WebDriver driver = new FirefoxDriver();
driver.get("https://demo.vtiger.com/");
driver.findElement(By.xpath("//button[text()='Sign in']")).sendKeys(Keys.ENTER);
driver.findElement(By.xpath("//button[text()='Sign in']")).submit();
}}
*We can also use submit() if button type=submit
<button type=”submit”>sign in</button>

*We can also use java script to click on a button

IQ10) How do you change the value present in the text box Ans:

Using clear() and sendkeys()

27
WebElement un=driver.findelement(By.id(“username”));

Un.clear();

Un.sendkeys(“bhanu”);

IQ11) How do you remove the value present in the text box without using clear() Ans:

un.sendkeys(Keys.CONTROL+”a”+Keys.DELETE);

IQ12)Write a script to copy paste the value present in one text box into another text box Ans:

un.sendkeys(Keys.CONTROL+”ac”+Keys. CONTROL+”v”);

IQ13)Write a script to print value present in the textbox.

Ans: WebElement un=driver.findElement(By.id(“username”));

String v=un.getAttribute(“value”);

System.out.println(v)

Limitation1:

In Selenium we cannot store the password in encrypted format.

IQ14)How do you retrieve tooltiptext of an element Ans:

using getAttribute(“title”)
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo01216 {

public static void main(String[] args)


{
WebDriver driver = new FirefoxDriver();
driver.get("https://demo.vtiger.com/");
WebElement chkBox=driver.findElement(By.name("remeber"));
String tt = chkBox.getAttribute("title");
System.out.println("tt");
}}
IQ15)Write a script to find the phone number of mubai in isrtc.com

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

public class Demo01216 {

28
public static void main(String[] args)
{
WebDriver driver = new FirefoxDriver();
driver.get("https://demo.vtiger.com/");
WebElement chkBox=driver.findElement(By.name("remeber"));
String tt = chkBox.getAttribute("title");
System.out.println("tt");

}
IQ16)What is the difference between getAttribute() & getText()

Ans: getAttribute() get the value of the specified attribute where as getText() is used to get the text of
the specified element

IQ17)Write a script to print x and y co-ordinates of an element.


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

public class Demo622016 {

public static void main(String[] args)


{
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost/login.do");
Point p=driver.findElement(By.id("username")).getLocation();
int x=p.getX();
int y=p.getY();
System.out.println(x);
System.out.println(y);
driver.close();

IQ18)Write a code to print height and width of the code


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

29
public class Demo622016 {

public static void main(String[] args)


{
WebDriver driver = new FirefoxDriver();
driver.get("http://www.gmail.com");
Dimension d=driver.findElement(By.id("next")).getSize();
int h=d.getHeight();
int w=d.getWidth();
System.out.println(h);
System.out.println(w);
driver.close();

}
IQ19)Write a code to print font size and color of the username text box in actiTime application
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo622016 {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://localhost/login.do");
WebElement un = driver.findElement(By.id("username"));
String fs = un.getCssValue("font-size");
System.out.println(fs);
String fc = un.getCssValue("color");
System.out.println(fc);
String ff = un.getCssValue("font-family");

System.out.println(ff);

driver.close();

OUTPUT:

14px

Rgba(0,0,0,1)

MS Shell Dlg\32

30
IQ20)Write a script to verify that login button is enabled
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Demo622016 {

public static void main(String[] args) {


WebDriver driver = new FirefoxDriver();
driver.get("http://localhost/login.do");
WebElement button = driver.findElement(By.id("loginButton"));
if(button.isEnabled())
{
System.out.println("Login Button is Enabled");
}
else
{
System.out.println("Login Button is not Enabled");
}

driver.close();

}
IQ21)Write a script to verify that Next button in the gmail login page is visble(hint:using isDisplayed())
IQ22)Write a script to verify whether keep me logged in checkbox present in facebook login page is
selected or not?(hint:isSelected())

Note:

1.isSelected() can be also used on radio button

2.Importent mathods of WebElement Interface

1. clear()
2. click()*
3. getAttribute()*
4. getCssValue()
5. getLocation()
6. getSize()
7. getTagName()
8. getText()*
9. isDisplayed()
10. isEnabled()
11. isSelected()

31
12. sendKeys()*
13. submit()

last page:
InvalidStateElementException(Unchecked Selenium Exception)

EXECUTING JAVA SCRIPT


Sometimes Selenium methods such as click(),sendKeys() etc.,may not work as an alternative option or
work around we can use java script.

EXECUTING JAVA SCRIPT MANUALLY:


Step#1:Open FireFox browser and open the required web page.press F12 which opens firebug window.

Step#2:Click on console tab

Step#3:Type the javascript statement in the text box which is available at rite the java script statement
in the text box which is available at the bottom of the firebug window and press enter

Hi..!!

OK

>alert(‘hi’)

EXECUTING JAVA SCRIPT PROGRAMMATICALLY:


To run the java scripts programmatically in selenium we should use excecuteScript() of
JavascriptExcecutor.Generally the object of the browser will be upcasted to WebDriver interface hence
excecuteScript()will be hidden.In order to access this method either we should downcast it to
RemoteWebDriver class or we should type cast it to JavascriptExcecuter interface

32
I I I
WEB DRIVER
JavascriptExecutor
excecuteScript(Str)
Close();

C RemoteWebDriver C

Close() {}
executeScript(Str){}

FirefoxDriver C C chromeDriver
else{} else{}
Executescript(Str){} executeScript(Str){}

IQ23)Write a code to click() on the button using java script

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

public class Demo062 {


Browser Constructor
public static void main(String[] args) {
Interface WebDriver driver = new FirefoxDriver(); Up-Casting
driver.get(“http://localhost/login.do”);
RemoteWebDriver r=(RemoteWebDriver) driver; Down casting
r.executeScript("document.getElementById('loginButton').click()");

}}

IQ24) Write a script to enter the text into text box without using sendKeys()
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.remote.RemoteWebDriver;

public class Demo062 {

33
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
driver.get("https://demo.vtiger.com/");
String c = "document.getElementById('username').value='abc'";
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript(c);
}

}
IQ25**) How do you enter the text if the text box is disabled using JavaScript
<html>

<body>

UN:<input id=”username” type=”text” disabled>

</body>

</html>
WebDriver driver = new FirefoxDriver();
driver.get("file:///c:/demo.html");
String c = "document.getElementById('username').value='bhanu'";
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript(c);

IQ26**) Write a script to scroll to the bottom of the page

WebDriver driver = new FirefoxDriver();


driver.get("http://news.google.com/");
String c = "window.scrollTo(0,document.body.scrollHeight)";
JavascriptExecutor j = (JavascriptExecutor) driver;
j.executeScript(c);

|||ly String c = "window.scrollTo(0,document.body.scrollHeight/2)"; for half

String c = "window.scrollTo(document.body.scrollWidth,0)"; for complete right

IQ27)Write a script to scroll to the element

(hint: find the x and y co-ordinates of the element using getLocation(),pass them as argument for javaScript)

Driver.findElement(By.id(“”)).getLocation()

Window.scrollTo(x,y)

IQ28)Write a script to take the photo of a application

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

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;

34
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.events.EventFiringWebDriver;

public class Demo062


{

public static void main(String[] args) throws IOException


{
WebDriver driver = new FirefoxDriver();
driver.get("http://localhost/license.jsp");
EventFiringWebDriver e=new EventFiringWebDriver(driver);
File srcFile = e.getScreenshotAs(OutputType.FILE);
File destFile = new File("c:/abc.png");
FileUtils.copyFile(srcFile, destFile);
driver.close();
}

NOTE:

Limitation:

*using selenium we can take screen shot in PNG(portable network graphics) format only.we can not take the
screenshot of popups,we cannot take screenshot of specific area in the page,we can not take the screenshot of
multiple browser or desktop

* If the page is very lengthy it will automatically takes the screenshot of complete page.

ENCAPSULATION
“Process of hiding the data and binding with methods is called as encapsulation.”

Example:
public class A

private int i;

public A()

i=10;

public int getValue()

35

You might also like