Wednesday, July 27, 2016

AUTOMATION 20 - GUI 8, Building a Selenium test from concept to execution

Technical difficulty: ****

After my initial script in Selenium, I wanted to move to a more complex script later on.  Along the way though I've encountered a few difficulties.

Today I'm not going to just talk you through my new script and what it does, but also looking at the problems I encountered and how I solved them.  This will help open the lid on how to solve problems yourself when you hit roadblocks creating a basic script.  [Hint - ninja Google skills]

The good news though - once you've overcome a problem once, you get better at avoiding it a second time!

Problem 1: Flawed concept

I originally created the post "I'm hoping that this blog will have the most comments" - hoping that just that, I'd be able to create a script which would be able to have you generate a comment on that blog.

The plan was to select "Anonymous" user, add a comment, and publish it.  Only of course, that's how spam happens - the kind of thing that goes "interesting blog - check out my link where I got 50% off the retail price of brand name shoes".

Because such behaviour is abused, there's a Captcha filter to ensure that you're really a human being.  [Of course ironically we're trying to do this without being a human being].

Problem 2: Where's the comment box?

It looks so simple - we should be masters at this - I used the developer tools to locate elements on the comment box, and set up commands ...

But when I ran it, it couldn't be found.  In fact it went horribly wrong ...

This took some digging around with developer tools to investigate - but I noticed the comment box was a #document which had it's own header and footer.  That is to say it was provided in another frame to the main one we've used to far!

Fortunately WebDriver has a command driver.switchTo().frame() to allow us to move through frames.  Typically the main frame we use is driver.switchTo().frame(0).

I used a very simple piece of code added to our last script

// Confirm if "Enter your comment" is there
if ( page_info.getPageSource().contains("Enter your comment"))
   System.out.println("Enter your comment");

I kept adjusting increasing the number in .frame() until I got "Enter your comment" found - then I knew I was in the right frame.

Extension material - try amending our previous script now, and see if you can get it to work for you.

Script brief

Because of these problems, the aim for the script has been slightly change, but that's all good (we learn more that way).

Our steps are as follows,

  • Load up my blog page, and select our desired frame.
  • Select "Anonymous" from the drop down list
  • Enter some text into the comment field
  • Select the Preview button
  • Confirm we see "Anonymous said" and the details of our comment

This gives us some nice experience using the frame selection, using drop-downs, entering text and selecting buttons.  Some core features covered!

I'll now take you through the new script published here a section at a time, explaining what each section does.  I do suggest you copy and paste it into Eclipse and have a bit of an experiment yourself using last time's guidelines.

WebDriver driver = new FirefoxDriver();
// Open our target page ... a previous blog articledriver.get("");

We pretty much covered this last time - declares a new WebDriver object, and opens the blog page.  Importantly, after last time's discussion, I've decided to use the nomenclature of "driver" for the object in keeping with most sample scripts you'll encounter, over the "page_item" I was using previously.

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
Because I was having issues when I couldn't find the comment box, I wasn't sure if my checks were occurring before the page had loaded, so just put a small wait in to be sure.

//Switch to the frame that comments are kept indriver.switchTo().frame(4);

As before, this moves us into the page frame where we know that comments are.

//Select to comment as "anonymous"
Select select = new Select( driver.findElement("identityMenu")) );//select.selectByIndex(8);select.selectByValue("ANON");

Useful to just mention - the WebElement is a subclass of WebDriver (a page is made of multiple WebElements after all).  Likewise the Select object is a subclass of a WebElement object for a drop down list.

This command allows us to select one of the drop down options - we can use .selectByValue which compares against a text value you can get from prying with the developer tools.

Commented out is the method .selectByIndex - this chooses the 8th element in the drop down list.  Try uncommenting it out, and commenting out the .selectByValue commend.  Indeed, try altering the command to pick different items in the drop down.

//Select the comment body fieldString stringComment= "This is my comment";WebElement element = driver.findElement("commentBodyField"));element.sendKeys(stringComment);
This selects the comment text box and types in "This is my comment" using the .sendKeys command.

 //Select to preview
element = driver.findElement("postCommentPreview"));

This finds the Preview button and uses the .submit command to press it.

Finally I use some .contains checks to confirm that I can see "Anonymous said" and "This is my comment" on the page.  You might wonder why I'm testing "Anonyous" and "said" separately - if you check the page html code, you'll see there's an invisible character between the two words which interferes with the .contains checking!

Extension material

We've covered a lot today, although to only a shallow level.  Do look up the commands - some of these I looked up by simple Google searches of "WebDriver select drop down" and seeing what examples were out there to try.

An odd quirk I found with the Firefox page opened by WebDriver, I'd usually be signed out of any webpages - that could be worth exploring.

Come up with ways to select other options to comment on my blog page.  Maybe even try and log in and generate a comment without needing Captcha.

As always - have fun whilst you learn.


  1. I take this is the experiment you were talking about :)

    Glad you tackled this from the scratch. It's something on my "would want to do" list, yet with the way how i'm struggling the "getting methods down" so i can get to what i want :/ this seems to be a distant future to me :D

    non the less - would be cool to pair up and see if there anything i might end up contributing :) Would love to be useful :D

  2. G club Casinos that make ordinary people rich millionaire.

    G club Is a leading website The online gambling bets that make ordinary people rich man millionaires have just opened your mind to play online gambling games, you will know that making money on the website of online casinos. This is very easy. Because after you subscribe to our online casino site, you will be able to play games, online gambling, everything you want to play.

    Today, playing online casino games is another fun new alternative that will make ordinary people become rich is not difficult at all. For online betting, there are many online gambling games that you can choose from. And the jackpot money. Including any other prize money more than a million you will receive every time after you apply for membership with our online casino site as well.

    For online gaming, online bets can be played 24 hours a day, and anytime you want. To know this, do not forget to join the fun and play online casino games you like. Join us and win prizes today at the website. Gclub มือถือ

  3. hello, I am priyanka from Switzerland. I m so happy to see this post in here. I really needed and fortunately, i found it in here. thank you.
    Digital Marketing Services in Delhi

  4. hello, I am Komal from Switzerland. I m so happy to see this post in here. I really needed and fortunately, i found it in here. thank you.
    Digital Marketing Services in Delhi

  5. Find experts for link building like thematic, one way link building & seo link building service provider company in Delhi & across India at cheap prices. On SMARTDIGITALWORK.COM, you can visit us : link building services Delhi, link building company India, link building services India, affordable link building services Delhi
    Link Building Services Delhi

  6. I have not encountered any problems. But thanks for the information presented.

    Golden Slot Android

  7. SMARTDIGITALWORK.COM - Offers digital marketing services like seo, smo, ppc. With web development, web designing, e-commerce site & mobile app development in Delhi &

    across India. You can reach us: digital marketing services in India, digital marketing services in Delhi, digital marketing company in Delhi, digital marketing company

    in India, smart digital work
    Digital Marketing Company in Delhi

  8. I am glad to see this post on your website. It is beneficial for us and can be very useful in our daily life, and we always use your website information and experiences. Anyway, thank you so much.
    digital marketing company in delhi

  9. Hi, I am Rahul thank you for this informative post.Thank you so much and for you all the best. ( That is a great job. Wish you more success. Takes Down

  10. Health Is God aims to deliver the best possible health reviews of the supplement collections and other wellness production that range from skincare to brain, muscle, male enhancement and brain health conditions. You, the user are of utmost importance to us, and we are committed to being the portal that sustains your healthy lifestyle. | Health And Fitness Tips

  11. XTR14 Testro is a great muscle boosting and sexual appetite boosting supplement that
    leads to deliver harder erection, great ejaculation hours, enhanced desires to have sex
    and make ripped shape muscle structure with a great boost of energy and strength level.
    It is available here for free trial basis so you need to avail it soon before stock
    gets out of the reach, hurry now and get going on the bed and at the gym confidently.
    XTR14 Testro


  12. I struggled to lose weight every time but as soon as I started using Pro Keto X
    things changed amazingly and I nearly lost around 5 pounds in two weeks of time.
    I would like to thank this dietary supplement that changed my life completely
    Keto Pro X

  13. The Great Info for Thank You!!
    We are an Auckland based digital marketing agency comprised of ad enthusiasts who strive to find the right marketing mix for our clients.

  14. Enduro stack Testosterone Booster is much safer than taking steroids or random supplements from the drugstore. Because, those supplements aren’t quality controlled or checked for safety, unlike this one. And, the majority of these products rely on artificial ingredients, fillers, and binders. Overtime, artificial ingredients can harm your body by building up toxins in it. Now, you can avoid that situation altogether with Enduro stack. Because, this supplement is completely natural, and it boosts testosterone the safe way. So, you can finally get results without worrying about hurting your body. Click below to order your own Enduro stack free trial!
    Enduro Stack

  15. Teal Farms Keto is one of the best weight loss supplement that is great to lose weight without causing any irritations to
    the health and makes the visibility of bad cholesterol formation get removed and you appear with a slim and stylish
    appearance with no more expression of hunger cravings or poor appetite level that is positive for the health
    factors on the long term basis Teal Farms Keto

  16. HealRun is a health news blog we provide the latest news about health, Drugs and latest Diseases and conditions. We update our users with health tips and health products reviews. If you want to know any information about health or health product (Side Effects & Benefits) Feel Free To ask HealRun Support Team.

  17. Pilpedia is supplying 100 percent original and accurate information at each moment of time around our site and merchandise, and the intent is to improve the usage of good and pure health supplement. For More Info please visit Pilpedia online store.

  18. Supplements For Fitness The Statistical and Diagnostic Manual of Mental Disorders (hereinafter, "DSM") is published by the American Psychiatric Association and describes a set of standard and language criteria for the classification of mental .

  19. Revuesdefaits defines personal characteristics of various health merchandise together with skincare, weight loss, muscle and male enhancement. The study presented here is briefly described for reader convenience and to deliver them assurance with health standards. The best potential answers are given here concerning the selection of a perfect supplement or cream or serum that presumably remains to be safe for health and do not cause any facet effects.

  20. Buyers Reviews defines personal characteristics of different health products including skincare, weightloss, muscle and male enhancement.Buyers Reviews is supplying 100% original and accurate information at each moment of time around our site and merchandise, and the intent is to improve the usage of good and pure health supplement.

  21. Healthy GNC - In usa is a wide variety of health,wellness and Male health performance products.which has include protein,male health performance and weight Loss management supplements.This product is really made to help improve your health, whether you are at the beginning of your fitness. Healthy GNC, gnc,weightloss, bodybuilding,vitamins, energy,fitness,strength,healthfulness, stamina, Wellness.
    For more info -

  22. Garcinia Shaping Pro supplement must weight loss the supplement. That supplement Garcinia Shaping Pro to helps on lose human weight on detoxify the your all body. That supplement been ablest to you’ll achieved the your body all desire. That supplement has been helps on burn on many all part calories highly potential.Garcinia Shaping Pro It customers to understand all supplement be known be skills facilitate on; losing your weight. Visit On

  23. Bitcoin is a digital payment currency that utilizes Canada Bitcoin Profit cryptocurrency (Web Bitcoin Club a digital medium of exchange) and peer-to-peer (P2P) technology to create and manage monetary transactions as opposed to a central authority. The Crypto Profit open source Canada Bitcoin P2P network creates the bitcoins and manages all the bitcoin transactions.

  24. This is a popular health resort for you. Relates to more than millions of products in the market. But we have suggestions for the most effective and healthy product and can facilitate health and fitness and most of you desire to understand it. You can feel free to ask data about the side effects and benefits of health and wellness items through our site.