Technical level: ****
To get the most out of this post, you will need a very basic level of Java (I hope to write a short series on this, but I have to get this epic series finished first).
If you're followed the preceeding steps, then you've installed all the software you need, and set up your Java project area with links to the Selenium WebDriver Jar files.
Now you're ready to try your first script!
There is a great simple script here, which I've seen a lot of sites use, and is the equivalent of "Hello World" for Selenium.
I actually wanted to have a script that did and checked for something, so created Script 1, which you can find on a previous post here. Open it up, and copy into Eclipse in your Java class (overriding what's there).
What we'll do is start by looking at what it does when run, and then taking a look at all the commands inside...
Hit run!
To make it run, hit the green play button here which will compile and build the code before running it ...
A Firefox browser pops up, open at the surreal entry I added about aardvarks last week ...
The output in Eclipse says this ...
If you have any problems, my advice is,
- Make sure Selenium WebDriver and Firefox are up to date.
- Check consistent class name - should be"PageCheck.java" and "public class PageCheck" match. Anything else, Google the error message. This is pretty much a developer masterclass in how they solve problems.
The script does the following,
- It opens Firefox and my Aardvark post
- It captures the page title, and prints to the screen
- It checks the whole page for the phase "blue aardvark" and prints it's found it - I mention "blue aardvark" in the article.
- It checks the whole page for the phase "red aardvark" and prints it's found it - I mention "red aardvark" in the comment.
- It checks the original post content for the phase "blue aardvark" and prints it's found it.
- It checks the original post content for the phase "red aardvark", finds nothing (it's in the comments), so prints nothing.
Although I've done a good job commenting it to make it readable, I'm now going to go through the commands used, and explain what they're all about, so you can understand how this script holds together.
import org.openqa.selenium.*;
There is a whole series of import commands, which brings import the parts of the Selenium JAR libraries that I need for our scripts.
In actual fact, I don't need the following right now, but likely to need them in script 2 - so left them in (a bad habit I know).
If I didn't use these import commands, I'd get all sorts of errors of items not find ... in fact I've commented it out and hit run, and I got ...
public class PageChecker
This declares the class we're using. To be honest with something so simple we're not really using it as a class, just so it can run right now. Classes are an important concept in modern languages like C# and Java, but too complicated to explore right now.
public static void main(String[] args)
This is the main function - that is the first thing which will be triggered when we hit run and the Java has compiled.
WebDriver page_item = new FirefoxDriver();
This sets an instance of a Firefox driver that we can use. Most code declares it as "WebDriver driver", but I felt that "WebDriver page_item" would be easier to follow - we can read the page content by looking at the data within page_item, and can likewise manipulate the page by calling page_item, methods.
It's on this command that a Firefox browser instance is triggered on your computer.
page_item.get("http://testsheepnz.blogspot.co.nz/2016/07/im-hoping-that-this-blog-will-have-most.html");
This causes the Firefox page_item to go to a web address, the address of my blog post.
System.out.println("Page title is: " + page_item.getTitle());
The command println prints to the command line. It prints out part of the page_item data, relating to the page title.
if ( page_item.getPageSource().contains("blue aardvark"))
This calls a page_item method, where it checks the page content for the text provided - "blue aardvark". If it's found it prints to screen it's been found.
The "red aardvark" if-statement is the same, just using a different search string.
WebElement element = page_item.findElement(By.id("post-body-8711143007795160191"));
A web element is a part of the page - we've declared it to be by id "post-body-8711143007795160191", which corresponds to the original post - not the title or the comments.
There are several alternative methods we could have used to declare the web element - such as by name or class ID. But we've already covered via our previous work using developer tools how to do this.
if ( element.getText().contains("blue aardvark"))
Very similar to the previous if-statement, only this time we're looking for the text string in the element, not the whole page.
Hence this time we'll find "blue aardvark" in the original post, but not "red aardvark".
Next time we'll look at how you can explore more Selenium commands, and we'll create a more complicated script.
Extension material
- Write a check for "See it run"
- Add a comment to that blog, and create a check for it
- Google other examples of Selenity commands that you can perform
먹튀뉴스 베팅커머스는 회원분들께 항상 새로운 정보를 드리도록 노력하고있습니다.
ReplyDeletedog kennel
ReplyDeleteGreat idea and good content keep post like this
ReplyDeleteEmbedded System Courses in Chennai
Embedded Training in Chennai
Looking forward to reading more. Great article post. Fantastic. Thanks so much for the blog. Much obliged.
ReplyDelete외국인출장
You actually make it seem so easy with your presentation but I find this matter to be really something which I think I would never understand. It seems too complex and extremely broad for me. I am looking forward for your next post, I’ll try to get the hang of it!
ReplyDelete마사지
What I was thinking about was solved thanks to your writing. I have written on my blog to express my gratitude to you.메리트카지노My site is We would be grateful if you visit us.
ReplyDeleteHey what a brilliant post I have come across and believe me I have been searching out for this similar kind of post for past a week and hardly came across this.
ReplyDelete토토
Hello, I am one of the most impressed people in your article. 우리카지노 What you wrote was very helpful to me. Thank you. Actually, I run a site similar to you. If you have time, could you visit my site? Please leave your comments after reading what I wrote. If you do so, I will actively reflect your opinion. I think it will be a great help to run my site. Have a good day.
ReplyDeleteYou have touched some pleasant factors here. Any way keep up writing. 야한동영상
ReplyDeleteClick this link
야설
Good Service. I'll come often. Great work! I'm surprised there's such a wonderful article 일본야동
ReplyDeleteClick this link
한국야동
Thanks for sharing. I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one. 한국야동닷컴
ReplyDeleteClick this link
국산야동
webgirls In terms of combating yeast infections, patients frequently have their operate eliminate to them. Simply because candida albicans can simply become chronic and ongoing. With that in mind, in the following paragraphs, we will current a variety of among the best established yeast infection treatment and prevention recommendations all around.
ReplyDelete