Handling dynamically generated elements in selenium webdriver via C#

935 views
Skip to first unread message

Vladimir Novoded

unread,
Jul 20, 2015, 2:34:18 PM7/20/15
to webd...@googlegroups.com
I am a new one to Selenium Webdriver. And I have practiced on different examples by myself.
I am trying to automate testing of a webpage that contains a list of items.
It's a button "Create" in Google Drive with expanding elements. I have used XPath locator to find button "Create"(driver.FindElement(By.XPath("")), but I don't know how to get access to the expanded elements and click on them by using C# and webdriver.
The code below is changing dynamically. When I hover over button "Create" in Google Drive
the code is like:
<div class ="j-Ta-pb f-e f-e-dg a-Da-e f-e-ea"
tabindex="0" role="button" aria-label="Create"
style="-moz-user-select:none;" guidedhelpid="new-menu-button" aria-expanded="false" aria-haspop="true">
<div class="j-Ta-pb f-e-rf" aria-hidden="true">Create</div>

when I click on button "Create", element aria-hidden has changed to "true" and another elements have added: aria-activedescendant=":4w" aria-owns=":4w" 

My problem is that I can't automate click on element "Folder" in combobox,that appears, after I automate action click on "Create" button.
Help me please!

darrell

unread,
Jul 21, 2015, 9:55:53 AM7/21/15
to webd...@googlegroups.com
Not something which is easily taught via a Google Group. Additionally, testing a static HTML site is a lot easier than testing something from Google. The Google applications are very jQuery, Ajax, Web 2.0, etc. heavy. If you are learning to use Selenium it is often easier to start on a simple site and work your way up to more advanced sites. If you go for automating Google sites you are often dealing with multiple issues and it is hard to separate them out.

I find automating sites like www.w3schools.com is not bad. You start on the reference sections. Then move to the static tutorials. If you want to get into dealing with frames (a more advanced topic) then you can start trying to automate the "Try It" sections of W3Schools. Once you nail those areas then dealing with dynamic content (Ajax enabled sites) isn't as hard.

Bottom line however, make sure you have a plan on what you want to test. Look at the site and create a step by step test case. Don't think about how you do it with Selenium. Just write out all the steps to test it regardless of whether you do it manually or automated. The step by step instructions need to be fairly detailed. Selenium is not smart. It is not artifical intelligence (AI). It isn't even an expert system. If you don't tell it EXACTLY what to do it will mess up. So before you automate the test you REALLY need to know all the steps. Once you know all the steps you can automate it. For example, to automate Google Drives:

  1. go to "https://www.google.com/drive/"
  2. click the "Go to Google Drive" button
  3. click the New button
  4. wait for the menu to open
  5. click the Folder option
  6. wait for the dialog to appear
  7. enter a name for the folder
  8. click the Create button
  9. wait for the folder to appear
  10. confirm you can enter the folder
Step 1 would be:


This assumes you have already setup the driver variable. Setting up the driver variable is just like opening the browser when manually testing. So REALLY step 1 is open the browser. :)

See how stupid Selenium is. We take for granted that if I tell you, a smart human, go to https://www.google.com/drive/, you KNOW you need to open a browser. Selenium isn't that smart. So I have to have a step 0, i.e. open the browser:

WebDriver driver = new FirefoxDriver();

Step 2 would be find the button and click it. So again, we know you have to find the button before you can click it. Selenium isn't that smart. I actually have to do two steps, 2a and 2b:

WebElement goToButton = driver.findElement(By.text("Go to Google Drive"));
goToButton.click();

Step 3 is similar. I need to find the New button and click it. But when I click the "Go to Google Drive", the page loads THEN all the content is rendered. If I try to find the button before it is rendered my test will fail. As a human I use my eyes. If the button has not appeared on the screen I wait up to a second for the button to appear then I click it. So my Selenium code has to wait for the button to be visible, find it then click it. This is Ajax stuff and why testing Google applications is hard. Waiting for Ajax calls in Selenium would be an entire chapter in a book.

Step 4 is also a Javascript (jQuery probably) call. You cannot just click the "Folder" option. You have to wait for the menu to open and the folder option to be visible. Coding this is also not a trivial thing.

Bottom line, you might look at my 10 step test case and thing that is very detailed but the truth is, this is not detailed enough for Selenium.

As someone learning Russian, I would probably think about what I wanted to say in English then translate it to Russian. After years I might learn to think in Russian. If I'm learning Selenium, I plan my tests out in English then translate them to Selenium. A complex concept in English might be a method call in my test automation. Then the method call needs to be broken down into multiple Selenium steps.
Reply all
Reply to author
Forward
0 new messages