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.
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"));
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!
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.