Skip to main content

You are here

Java

Capture a section of the screen using Selenium Webdriver

Capturing a section of the screen involves:

1) Performing a screen capture as usual and saving the created image to disk.
2) Getting the saved image and cutting/ cropping out a section of it as required into another image file.

Tags: 

Keyboard operations in Selenium WebDriver

The Keys class library allows us to reference the non alphabet/ text keys on the keyboard. This applies to keyboard keys like the ARROW keys, ALT, CTRL etc. Of course for text entries like "John", we can simply write:


WebElementVariable.sendKeys("John");

The following codes will call the browser, load the google website and then simulate making an entry of "1999" into the search text box.

Automate the log in steps for this website.

The following would be the manual steps required to log on to the test user account for "TestLogin". I have created this account for this exercise.

Username: TestLogin
Password: TestPassword

The manual steps are:

Navigation using the browser Back or Front button

The following code programmatically clicks on the browser's "Front" or "Back" navigation buttons to move between visited pages. Between each navigation is a brief waiting period to slow the process down so its noticeable.

Scroll bars dragging

The following code demonstrates how to drag the vertical scroll bars on a window. The "Actions" class is used to click and hold on to the scroll bar and then it is dragged to a new location.

Switch between two open windows or browser tabs

The following code demonstrates the SwitchTo() function. A popup window is opened from a main window and then control or focus is switched between these windows. This is particularly useful for when a test calls up a second window or popup and testing continues on the newly opened window. This code also demonstrates the use of an "Iterator".

Check if a page element is enabled or not

The following code checks if an item is enabled. If an element is not enabled then certain operations like Click() cannot be performed on it. The following code

Various ways to locate an element on a page

These are the various way to locate or find elements on a page. I made the compilation from the SeleniumHQ website. Very handy snippets for the office.

By ID
====

This is the most efficient and preferred way to locate an element. Common pitfalls that UI developers make is having non-unique id’s on a page or auto-generating the id, both should be avoided. A class on an html element is more appropriate than an auto-generated id.
Example of how to find an element that looks like this:
<div id="firstname">...</div>

Tags: 

Waiting for conditions to be available

While testing a very slow loading website, the dreaded "NoSuchElementException" exception can occur even though the element does exist on the page. The problem can be that the code is running faster than the site loads. The "ExpectedCondition" class ensures that the program waits for a designated period of time before throwing the "NoSuchElementException" error message.

In the code below, we navigate to craigslist, take 5 seconds to wait for the required element to appear and be ready for clicking. Then click the element.

Assert if an option exists on a menu

This is kind of a crude method but its worked. This is an extention of the codes shown in the previous post. I mentioned I would re-write the codes to check for the presence of a text or option in a menu.

I have set reference to TestNG on this one. So in addition to the Selenium library, the TestNG library should be referenced as well.

The stepts I have adopted include:

Tags: 

Pages

Premium Drupal Themes by Adaptivethemes