30. click( ) - predefined method to click links and buttons







click( ) is a predefined method of 'Selenium' Class which is used to click on links and buttons.

Lets Implement This:

Pre-requisites:

1. Create a new Project say 'RC-Project2' in Eclipse IDE
2. Configure the Project to work with Selenium RC
3. Configure the Selenium Standalone Server to run from Eclipse IDE
4. Start the Selenium Standalone Server
5. Create a package say 'package_two' under the newly created project.
6. Create a Java Class file say 'Class2' under the newly created package as shown below:



Actual Steps:

1. Write the following code into the newly created Java Class file as shown below and make sure that you resolve all the errors before going to next step:

2. The above code will starts the Selenium RC Driver, opens the http://book.theautomatedtester.com in the Test Browser window and finally it maximizes the Test Browser window as shown below:


3. Now lets write the Selenium RC Automation code using any Selenium Command that clicks the 'Chapter1' link in the above screen. But in order to click the 'Chapter1' link, we have to first find the locator of the 'Chapter1' link. How to find the locators for the UI elements is explained in our previous posts. Please go through the below previous posts before going to the next step:

Post#19 What are Locators?
Post#20 Different Types of Locators to identify the UI elements
Post#21 Tools in Selenium to find out the Locators.
Post#22 Locate UI elements by ID
Post#23 Locate UI elements by Name
Post#24 Locate UI elements by Link
Post#25 Locate UI elements by XPath
Post#26 Locate UI elements by CSS
Post#27 Finding Locators for UI elements using the Selenium IDE's recording feature

4. Using FireBug options, Inspect the 'Chapter1' link and identify the locator after observing the below code in the below screenshot:


5. After seeing the above html code in the screenshot, its very clear that we've to identify the 'Chapter1' link using link locator using the syntax link=LinkName  i.e. link=Chapter1 in this case.
6. Also we've to use the above link locator in the Selenium RC Command which is used to click the specified link. object1.click("Locator") is the syntax we've to use for clicking the specified locator.
7. So after understanding the things in step5 and step6, its very clear that we've to write object1.click("link=Chapter1"); to click the 'Chapter1' link as shown below:


8. Start the Selenium Standalone Server
9. Save and Run the 'Class2.java' file by selecting the 'JUnit Test' option and ensure that the 'Chapter1' link is clicked and as a result 'Chapter1' page is displayed as shown below:


Watch the below video:

Click here to watch the video.

Download this Project:

Click here to download this project and import into Eclipse IDE  on your machine.

10. Till now the code has clicked on 'Chapter1' link and navigated  to the 'Chapter1' page. Now lets write code for click on the button 'load text to the page' in 'Chapter1' page and ensure that 'I have been added with a timeout' text is displayed in the text box on clicking the button.  (i.e. 'load text to the page' button is designed in such a way that on clicking it, it displays 'I have been added with a timeout' text in the adjacent text box)
11. Using Firebug options, inspect the 'load text to the page' button in the 'Chapter1' page as shown below:



12. After seeing the above html code in the screenshot, its very clear that we've to identify the 'load text to the page' button using ID locator using the syntax id=Element_Id  i.e. id=secondajaxbutton in this case.
13. Also we've to use the above ID locator in the Selenium RC Command which is used to click the specified button. object1.click("Locator") is the syntax we've to use for clicking the specified locator.
14. So after understanding the things in step6 and step7, its very clear that we've to write object1.click("id=secondajaxbutton"); to click the 'load text to the page' button as shown below:


15. Save and Run the 'Class2.java' file by selecting the 'JUnit Test' option and ensure that 'I have been added with a time out' text is displayed in the 'Chapter1' page -> text box as shown below:


16. As 'I have been added with a timeout' text is not displayed in the text box, its very clear that our test has not clicked the 'load text to the page' button and failed as shown below:


17. What was the problem? Why did the test failed ? The answer is our automation code has tried to click on the 'load text to the page' button before the 'load text to the page' button element is loaded in the 'Chapter1' page. So how to resolve this problem. The only solution to this problem is to wait for the page to load by giving it some time using Thread.sleep(MilliSeconds) Java statement before the command which clicks the 'load text to the page' button as shown below:


18. Repeat the step 15 after adding the Thread.sleep(3000); to the code and observe that the button is click and the desired text is added to the text box as shown below:




Watch the below video:


Click here to watch the video.

Download this Project:

Click here to download this project and import into Eclipse IDE  on your machine.



Please comment below to feedback or ask questions.

Using type( ) method will be explained in the next post.



No comments:

Post a Comment

Followers

Labels