Skip to main content

You are here

Selenium WebDriver

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:


Hover the mouse on a menu and click on a resulting dropdown menu option

This piece of code allows you find and click on a drop down menu resulting from hovering the mouse over a main menu option. The issue with a menu resulting from hovering the mouse is that the sub menu elements are not available for selection until the drop down menu appears. As long as focus remains on the menu, one can choose from the drop down.

In the program below, I have used the Actions class to initiate mouse movement. The steps taken are:

List all the options in a menu

The following loads a website then prints off the text on the links in the main menu. This is a very simple code. Its actually not of very much use except it can be adapted to Assert if certain menu links exist. I am re-writting it to check for the presence of designated menu options. For example to check if the menu contains "Home" or "Forums". Watch out for that next.


Add up numbers on a table using Selenium

I made a few assumptions on this one. For example the number of rows. Am sure there is a way to determine the row end. Otherwise watch the console as the code runs. It lists the numbers and adds a sum at the end.

Taking Screen Shot using Selenium WebDriver

The following code takes a screen shot of the website First, the Firefox browser is opened, then the site is loaded. The screen shot is taken and stored in a folder on the C:logicandtricks. Ensure you edit the location for the screen shot file as you see fit on the line:

FileUtils.copyFile(screenshot, new File("c:\\logicandtricks\\screenshot.jpg"));

To run the program you need to have Selenium configured.

Have fun!

How to simulate mouse movement in Selenium WebDriver

This code navigates to a website It then simulates moving the mouse from one menu link to the other. Observe the dropdown menu as focus shifts from one menu option to the other. There is a slight delay between each menu option (curtesy of Thread.sleep).

For some strange reason a previously visited menu link's dropdown remained even after focus shifted. Hoping some gurus can explain that to us in the comments sections. smiley


Premium Drupal Themes by Adaptivethemes