Selenium 2.0 and WebDriverNOTE:
We’re currently working on documenting these sections. We believe the information here is accurate, however be aware we are also still working on this chapter. Additional information will be provided as we go which should make this chapter more solid. In addition, we will be proofreading and reviewing it. Selenium 2.0 FeaturesSelenium 2.0 has many new exciting features and improvements over Selenium 1. These new features are introduced release in the release announcement in the Official Selenium Blog. The primary new feature is the integration of the WebDriver API. This addresses a number of limitations along with providing an alternative, and simpler, programming interface. The goal is to develop an object-oriented API that provides additional support for a larger number of browsers along with improved support for modern advanced web-app testing problems. NOTE: We will add a description of SEL 2.0 new features–for now we refer readers to the release announcement. The Selenium Server – When to Use ItYou may, or may not, need the Selenium Server, depending on how you intend to use Selenium. If you will be strictly using the WebDriver API you do not need the Selenium Server. The Selenium Server provides Selenium-RC functionality, which is primarily used for Selenium 1.0 backwards compatability. Since WebDriver uses completely different technology to interact with the browsers, the Selenium Server is not needed. Selenium-WebDriver makes direct calls to the browser using each browser’s native support for automation. Selenium-RC however requires the Selenium- Server to inject javascript into the browser and to then translate messages from your test program’s language-specific Selenium client library into commands that invoke the javascript commands which in turn, automate the AUT from within the browser. In short, if you’re using Selenium-WebDriver, you don’t need the Selenium-Server. Another reason for using the Selenium-Server is if you are using Selenium-Grid for distributed exectution of your tests. Finally, if you are using Selenium-backed Web-Driver (the WebDriver API but with back-end Selenium technology) you will also need the Selenium Server. These topics are described in more detail later in this chapter. Setting Up a Selenium-WebDriver ProjectTo install Selenium means to set up a project in a development so you can write a program using Selenium. How you do this depends on your programming language and your development environment. Getting Started With Selenium-WebDriver WebDriver is a tool for automating testing web applications, and in particular to verify that they work as expected. It aims to provide a friendly API that’s easy to explore and understand, easier to use than the Selenium-RC (1.0) API, which will help make your tests easier to read and maintain. It’s not tied to any particular test framework, so it can be used equally well in a unit testing or from a plain old “main” method. This section introduces WebDriver’s API and helps get you started becoming familiar with it. Start by setting up a WebDriver project if you haven’t already. This was described in the previous section, Setting Up a Selenium-WebDriver Project. Once your project is set up, you can see that WebDriver acts just as any normal library: it is entirely self-contained, and you usually don’t need to remember to start any additional processes or run any installers before using it, as opposed to the proxy server with Selenium-RC. You’re now ready to write some code. An easy way to get started is this example, which searches for the term “Cheese” on Google and then outputs the result page’s title to the console. how to learning script by Java: plz notice why we used which lines. That make you understand how to learn scripting package org.openqa.selenium.example; // importing selenium in code space import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.ExpectedCondition; import org.openqa.selenium.support.ui.WebDriverWait; public class Selenium2Example { public static void main(String[] args) { // Create a new instance of the Firefox driver // Notice that the remainder of the code relies on the interface, // not the implementation. WebDriver driver = new FirefoxDriver(); // And now use this to visit Google driver.get("http://www.google.com"); // Alternatively the same thing can be done like this // driver.navigate().to("http://www.google.com"); // Find the text input element by its name WebElement element = driver.findElement(By.name("q")); // Enter something to search for element.sendKeys("Cheese!"); // Now submit the form. WebDriver will find the form for us from the element element.submit(); // Check the title of the page System.out.println("Page title is: " + driver.getTitle()); // Google's search is rendered dynamically with JavaScript. // Wait for the page to load, timeout after 10 seconds (new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() { public Boolean apply(WebDriver d) { return d.getTitle().toLowerCase().startsWith("cheese!"); } }); // Should see: "cheese! - Google Search" System.out.println("Page title is: " + driver.getTitle()); //Close the browser driver.quit(); }} Moving Between Windows and Frames Some web applications have any frames or multiple windows. WebDriver supports moving between named windows using the “switchTo” method: driver.switchTo().window("windowName");All calls to driver will now be interpreted as being directed to the particular window. But how do you know the window’s name? Take a look at the javascript or link that opened it: <a href="somewhere.html" target="windowName">Click here to open a new window</a> Alternatively, you can pass a “window handle” to the “switchTo().window()” method. Knowing this, it’s possible to iterate over every open window like so:for (String handle : driver.getWindowHandles()) { driver.switchTo().window(handle); } You can also swing from frame to frame (or into iframes):driver.switchTo().frame("frameName");It’s possible to access subframes by separating the path with a dot, and you can specify the frame by its index too. That is: driver.switchTo().frame("frameName.0.child");would go to the frame named “child” of the first subframe of the frame called “frameName”.All frames are evaluated as if from *top*. User Input - Filling In FormsWe’ve already seen how to enter text into a textarea or text field, but what about the other elements? You can “toggle” the state of checkboxes, and you can use “click” to set something like an OPTION tag selected. Dealing with SELECT tags isn’t too bad: WebElement select = driver.findElement(By.xpath("//select")); List<WebElement> allOptions = select.findElements(By.tagName("option")); for (WebElement option : allOptions) { System.out.println(String.format("Value is: %s", option.getAttribute("value"))); option.click(); } This will find the first “SELECT” element on the page, and cycle through each of its OPTIONs in turn, printing out their values, and selecting each in turn. As you will notice, this isn’t the most efficient way of dealing with SELECT elements. WebDriver’s support classes include one called “Select”, which provides useful methods for interacting with these. Select select = new Select(driver.findElement(By.xpath("//select"))); select.deselectAll(); select.selectByVisibleText("Edam"); This will deselect all OPTIONs from the first SELECT on the page, and then select the OPTION with the displayed text of “Edam”. Once you’ve finished filling out the form, you probably want to submit it. One way to do this would be to find the “submit” button and click it: driver.findElement(By.id("submit")).click(); // Assume the button has the ID "submit" :)Alternatively, WebDriver has the convenience method “submit” on every element. If you call this on an element within a form, WebDriver will walk up the DOM until it finds the enclosing form and then calls submit on that. If the element isn’t in a form, then the NoSuchElementException will be thrown:
71 Comments
setup eclipse & selenium
9/27/2011 05:31:40 pm
i download selenium-2.7.0 (java) and Eclipse IDE for Java Developers,
Reply
Masud
9/27/2011 05:33:51 pm
now do the followings
Reply
i can run
9/27/2011 06:43:38 pm
Reply
i can run
9/27/2011 06:44:29 pm
after go to step 5. i have many bug ^^
Reply
Masud
9/27/2011 06:49:08 pm
you have script error
Reply
Vince
8/28/2016 05:11:19 pm
How I set the selenium in the build path..I am new to this one and get the same error.
Reply
Phuc
9/29/2011 07:28:47 pm
i want to know about code of action click
Reply
Masud
9/29/2011 07:33:19 pm
two way to do that
Reply
Masud
9/29/2011 07:34:39 pm
if you want to find by id then just use
Reply
phuc
10/2/2011 07:31:04 pm
driver.findElement(By.name("pmenuM")).click();
Reply
Masud
10/2/2011 07:32:27 pm
this is not the error trace.. see the left down side of your IDE u will see an error trace box.. copy that error and send me
Reply
pham
10/13/2011 04:18:56 pm
thank you man for your all information you shared here.
Reply
Quynh
10/16/2011 05:40:48 pm
i can not see the test result. how can I see that ?
Reply
Masud
11/9/2011 03:23:25 pm
@Quynh: please turn on the junit result box
Reply
Ritesh
1/6/2012 11:57:45 pm
Hey Masud,
Reply
Masud
1/8/2012 12:23:46 pm
@Ritesh : hay ritesh. Your question a bit confusing. Do u want to automate android web application ? If yes then go though my this post , Link: http://masudparvez.weebly.com/1/post/2011/08/andriod-webdriver-want-to-test.html
Reply
7/18/2012 05:43:27 pm
I was recommended this blog by my cousin. I am not confident whether this post is written by him as nobody else know such detailed about my difficulty. You are incredible! Thanks!
Reply
masud
9/16/2012 03:23:43 pm
Thank you very much for your great comment. :)
Reply
9/16/2012 03:12:40 pm
kudos! A trustworthy blog, thanks for putting an effort to publish this information. very informative and does exactly what it sets out to do. thumbs up! :)
Reply
Masud
9/16/2012 03:25:31 pm
Hay Joseph. thanks for your nice comment !
Reply
4/1/2013 03:13:44 am
I gotta bookmark this web site it seems very beneficial very beneficial
Reply
Masud
11/24/2015 08:07:54 pm
Thank you very much for your compliment.
Reply
11/24/2015 04:16:32 am
Hi, I have read your blog and I gathered some needful information from this blog. Thanks for sharing informative post. Keep update your blog.
Reply
Masud
11/24/2015 08:08:21 pm
Thank you very much. I am always happy to help.
Reply
12/17/2015 03:25:16 am
Hi, You have given really informative post. Thanks for sharing this blog.
Reply
Masud
12/17/2015 11:07:49 pm
You are most welcome !
Reply
1/23/2016 04:23:26 am
The future of software testing is on positive note. It offers huge career prospects for talented professionals to be skilled software testers.
Reply
Masud
1/24/2016 10:54:06 pm
You are absolutely right !
Reply
Masud
4/22/2016 01:42:14 am
Thank you.
Reply
Masud
4/22/2016 01:41:43 am
Thank you !
Reply
4/22/2016 03:14:34 am
I have read your blog. It’s very informative and useful blog. You have done really great job. Keep update your blog.
Reply
Masud
4/26/2016 12:33:07 am
Thank you and it was my pleasure !
Reply
4/24/2016 10:40:45 pm
The Information which you provided is very much useful for me. Thank You for Sharing Valuable Information.I like this blog and this is very informative. Keep sharing..
Reply
4/26/2016 10:40:42 pm
Hi, This is really nice blog. You have done really great job. Keep posting.
Reply
I had the following errors:
Reply
5/18/2017 10:38:06 pm
Reply
5/18/2017 11:44:31 pm
Reply
3/23/2018 01:22:59 am
Great posting with useful topics.Thank you.
Reply
4/2/2018 10:40:50 pm
A very interesting case study
Reply
8/27/2018 05:20:33 am
I am really happy with your blog because your article is very unique and powerful for new reader.
Reply
8/27/2018 05:23:39 am
I am really happy with your blog because your article is very unique and powerful for new reader.
Reply
10/5/2018 04:08:34 am
Thanks for your efforts in sharing this information in detail. This was very helpful to me. kindly keep continuing the great work.
Reply
10/5/2018 04:09:11 am
<a href="https://www.fita.in/ielts-coaching-chennai/">IELTS coaching in Chennai</a>
Reply
mercyroy
10/8/2018 10:01:27 pm
Reply
Nice articles posted. It's useful for developing my skill. Keep sharing the articles!!!
Reply
10/9/2018 11:37:10 pm
I wish to indicate because of you only to bail me out of this specific trouble. As a consequence of checking through the net and meeting systems that were not beneficial, I thought my life was finished.
Reply
10/10/2018 03:50:05 am
Thank you for the information.
Reply
Amazing Blog. I liked your style of writing. Pls keep on writing.
Reply
10/18/2018 08:57:28 pm
Innovative thinking of you in this blog makes me very useful to learn.i need more info to learn so kindly update it.
Reply
10/19/2018 02:23:30 am
It is very excellent blog and useful article thank you for sharing with us, keep posting.
Reply
10/22/2018 12:04:08 am
Amazing Post. It shows your great in-depth knowledge on the topic. Thanks for Posting. You are a life-saver.
Reply
I am really enjoying reading your well written articles.
Reply
11/13/2018 10:03:04 pm
And indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
Reply
11/30/2018 11:20:28 pm
Extra-Ordinary. The way the blog was written is amazing. Waiting for your next post.
Reply
12/31/2018 12:17:40 am
Nice post..Thanks for sharing your informative article.,
Reply
1/11/2019 01:56:56 am
It was a nice blog..The informations are so useful.. keep sharing..
Reply
Jayalakshmi
8/20/2020 04:44:34 am
Good tutorial. Thanks for sharing the valuable information. it’s really helpful. Who want to learn this blog most helpful. Keep sharing on updated tutorials…
Reply
jeni
8/21/2020 06:42:13 am
Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites!
Reply
shiny
8/23/2020 04:44:31 am
Excellent and very cool idea and the subject at the top of magnificence and I am happy to this post..Interesting post!
Reply
Praveen
8/23/2020 11:10:53 pm
Excellent post, From this post i got more detailed information,Thanks to share with us,
Reply
5/25/2022 08:06:06 am
Hi Masud Parvez, Amazing content on Selenium webdriver and the detailed features of the Selenium Version 2.0. Thank you for giving clear content on Selenium web drive, and the code snipped helps me to work on it!! Kudoos to you!!!
Reply
Leave a Reply. |
AuthorI am Masud Parvez. Working as IT Senior Project Manager for RMIT University. Previously I built and run a distributed Test Center. My success was to turn that in to one of the most successful business units of the company. Categories
All
|