Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

 Verify Element Present or not:

if(driver.findElements(By.linkText("REGISTER")).size()!=0){
{

System.out.println("element is present");
}
else{

System.out.println("element is not present");


}

 Verify Element Is Enabled or Not:


isEnabled() method is used to check element is enabled or not.

if(driver.findElement(By.linkText("REGISTER")).isEnabled()){
System.out.println("Element is Enable");
}else{
System.out.println("Element is Disabled");
}

 Capturing Entire Page Screenshot:


Screenshots are desirable for bug analysis. Selenium can
automatically take screenshots during execution. You need to type cast
WebDriver instance to TakesScreenshot.

Taking Screenshot in Selenium is a 3 Step process

Step 1) Convert web driver object to TakeScreenshot

TakesScreenshot scrShot =((TakesScreenshot)webdriver);

Step 2) Call getScreenshotAs method to create image file

File SrcFile=scrShot.getScreenshotAs(OutputType.FILE);

Step 3) Copy file to Desired Location

Example: In this example we will take screenshot


http://demo.guru99.com/V4/ & save it as C:/Test.png
TakesScreenshot screenshot=((TakesScreenshot)driver);
File srcfile=screenshot.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(srcfile, new File("D://eclipse//test1.png"));

 Generating Mouse Hover Event:


We are going to present how you can mouseover an web
element using Selenium WebDriver.
1. First we identify the element to be hovered in the web page
2. hover (mouseover) functionality is provided in Selenium WebDriver with the
help of the Actions class, which provides the ability to move the mouse over
an element.
WebElement
element=driver.findElement(By.xpath("//span[contains(text(),'TVs &
Appliances')]"));
action.moveToElement(element);
action.perform();
driver.findElement(By.xpath("//a[contains(text(),'Automatic Front
Load')]")).click();

 Handling Multiple Windows:


What is Alert: Alert is a small message box which displays on-screen
notification to give the user some kind of information or ask for permission to
perform certain kind of operation. It may be also used for warning purpose.

1. Simple alert: This simple alert displays some information or warning


on the screen.
2. Prompt alert: This Prompt Alert asks some input from the user
and selenium webdriver can enter the text using sendkeys(" input…. ").

3. Confirmation alert: This confirmation alert asks permission to do some


type of operation.
How to handle Alert in Selenium WebDriver
Alert interface provides the below few methods which are widely used in Selenium
Webdriver.

1) void dismiss() // To click on the 'Cancel' button of the alert.

driver.switchTo().alert().dismiss();

2) void accept() // To click on the 'OK' button of the alert.

driver.switchTo().alert().accept();

3) String getText() // To capture the alert message.

driver.switchTo().alert().getText();

4) void sendKeys(String stringToSend) // To send some data to alert box.

driver.switchTo().alert().sendKeys("Text");
Scenario:
Step 1) Launch the web browser and open the site

driver.get("http://demo.guru99.com/test/delete_customer.php");

Step 2) Enter Any Customer id.

driver.findElement(By.name("cusid")).sendKeys("53920");

Step 3) After entering the customer ID, Click on the "Submit" button.

driver.findElement(By.name("submit")).submit();
Step 4) Reject/accept the alert.

// Switching to Alert
Alert alert = driver.switchTo().alert();

// Capturing alert message.


String alertMessage= driver.switchTo().alert().getText();

// Displaying alert message


System.out.println(alertMessage);

// Accepting alert
alert.accept();

How to handle Selenium Pop-up window using


Webdriver:
In automation, when we have multiple windows in any web application, the activity
may need to switch control among several windows from one to other in order to
complete the operation. After completion of the operation, it has to return to the main
window i.e. parent window. We will see this further in the article with an example.

In selenium web driver there are methods through which we can handle multiple
windows.

Driver.getWindowHandles();

To handle all opened windows by web driver, we can use


"Driver.getWindowHandles()" and then we can switch window from one window to
another in a web application. Its return type is Iterator<String>.

Driver.getWindowHandle();

When the site opens, we need to handle the main window


by driver.getWindowHandle(). This will handle the current window that uniquely
identifies it within this driver instance. Its return type is String.

To handle multiple windows in Selenium WebDriver, We follow the following steps.

Now, we will automate the given below scenario to see how to handle multiple
windows using Selenium Webdriver.

Step 1) Launch the site.

Launch the browser and open the site " http://demo.guru99.com/popup.php "

driver.get("http://demo.guru99.com/popup.php");

Step 2) Click on link "Click Here".

When the user clicks on the "Click Here" link, new child window opens.
driver.findElement(By.xpath("//a[contains(text(),'Click Here')]")).click();

Step 3) New Child Window opens.

A new window opens, ask the user to enter email id and submit the page.

Step 4) Enter your email ID and submit.


Step 5) Display the Access Credentials on submitting the page.

Step 6) Close the child window

Step 7) Switch to the parent window


driver.get("http://demo.guru99.com/popup.php");
driver.findElement(By.xpath("//a[contains(text(),'Click Here')]")).click();

String MainWindow=driver.getWindowHandle();
// To handle all new opened window.
Set<String> s1=driver.getWindowHandles();
Iterator<String> i1=s1.iterator();
while(i1.hasNext())
{
String ChildWindow=i1.next();
if(!MainWindow.equalsIgnoreCase(ChildWindow))
{

// Switching to Child window


driver.switchTo().window(ChildWindow);

driver.findElement(By.name("emailid")).sendKeys("sujatajalkote@gmail.com");
driver.findElement(By.name("btnLogin")).click();
driver.close();
}
}

Reading Font Properties Using .getCssValue():


RGBA color values are an extension of RGB color values with an alpha
channel - which specifies the opacity for a color.

An RGBA color value is specified with: rgba(red, green, blue, alpha). The alpha
parameter is a number between 0.0 (fully transparent) and 1.0 (fully opaque).

//Read font-size property and print It In console.


String fontSize = text.getCssValue("font-size");
System.out.println("Font Size -> "+fontSize);

//Read color property and print It In console.


String fontColor = text.getCssValue("color");
System.out.println("Font Color -> "+fontColor);

//Read font-family property and print It In console.


String fontFamily = text.getCssValue("font-family");
System.out.println("Font Family -> "+fontFamily);

//Read text-align property and print It In console.


String fonttxtAlign = text.getCssValue("text-align");
System.out.println("Font Text Alignment -> "+fonttxtAlign);

How to handle waits (implicit/explicit/fluent):


1. Why Do We Need Waits In Selenium?

Most of the web applications are developed using Ajax and


Javascript. When a page is loaded by the browser the elements which we want to
interact with may load at different time intervals.

Not only it makes this difficult to identify the element but also if the element is not
located it will throw an "ElementNotVisibleException" exception. Using Waits, we
can resolve this problem.

2. Implicit wait:
The implicit wait will tell to the web driver to wait for certain amount of
time before it throws a "No Such Element Exception". The default setting
is 0. Once we set the time, web driver will wait for that time before
throwing an exception.

driver.manage().timeouts().implicitlyWait(TimeOut, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS) ;

Implicit wait will accept 2 parameters, the first parameter will accept
the time as an integer value and the second parameter will accept the time
measurement in terms of SECONDS, MINUTES, MILISECOND,
MICROSECONDS, NANOSECONDS, DAYS, HOURS, etc.

3. Explicit wait:
The explicit wait is used to tell the Web Driver to wait for certain
conditions (Expected Conditions) or the maximum time exceeded before
throwing an "ElementNotVisibleException" exception.

The explicit wait is an intelligent kind of wait, but it can be applied only for
specified elements. Explicit wait gives better options than that of an
implicit wait as it will wait for dynamically loaded Ajax elements.
Once we declare explicit wait we have to use "ExpectedCondtions" or we
can configure how frequently we want to check the condition using Fluent
Wait.

WebDriverWait wait = new WebDriverWait(WebDriverRefrence,TimeOut);

WebDriverWait is a class.

4. Fluent wait:
The fluent wait is used to tell the web driver to wait for a condition, as
well as the frequency with which we want to check the condition before
throwing an "ElementNotVisibleException" exception.
Frequency: Setting up a repeat cycle with the time frame to
verify/check the condition at the regular interval of time

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)


.withTimeout(30, TimeUnit.SECONDS)
.pollingEvery(5, TimeUnit.SECONDS)
.ignoring(NoSuchElementException.class);
In the above example, we are declaring a fluent wait with the timeout
of 30 seconds and the frequency is set to 5 seconds by ignoring
"NoSuchElementException"

 Difference between Implicit Wait, Thread.sleep, Explicit Wait and


Fluent Wait

Properties Implicit Wait Thread.sleep Explicit Wait Fluent Wait


Timeout Supported Supported Supported Supported
Condition Check Not Supported Not Supported Supported Supported
Criteria Applicable to all Not Supported Applicable for specific Applicable for
pages and elements element specific element
Ignore exception Not Supported Not Supported Not Supported Supported
Repetitive Not Supported Not Supported Not Supported Supported
condition check

You might also like