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

page_info.switchTo().frame(4);
// 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("http://testsheepnz.blogspot.co.nz/2016/07/im-hoping-that-this-blog-will-have-most.html");

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(By.name("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(By.id("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(By.id("postCommentPreview"));
element.submit();

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.

16 comments:

  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

    ReplyDelete
    Replies
    1. Absolutely keen on pairing up with you Guna - we just need to get our "to do" list sorted a bit. 8-)

      Delete
  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 มือถือ

    ReplyDelete
  3. 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

    ReplyDelete
  4. 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

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






    Golden Slot Android

    ReplyDelete

  6. 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

    ReplyDelete
  7. 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.

    ReplyDelete
  8. 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.

    ReplyDelete
  9. 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.

    ReplyDelete
  10. 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 .

    ReplyDelete
  11. 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.

    http://revuesdefaits.fr/testoultra/

    ReplyDelete
  12. 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 - http://www.healthygnc.com/

    ReplyDelete
  13. 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 http://www.powerenrich.com/garcinia-shaping-pro-free-trial-lose-weight-the-most-natural-way/

    ReplyDelete
  14. How to buy bitcoin. A growing number of people across the globe are requesting this specific question. This really is because of the fact that the coin rate is perpetually increasing. It's a risky investment but in addition a superior yield expenditure. Trading Bit coin pays off on all sides. Many businesses are introducing crypto currencies as a kind of cost for both items and services. It is absolutely the potential for payments.

    ReplyDelete