named this program as the “JavaScriptTestRunner.” Seeing potential in this idea to help automate other web applications , he made JavaScriptRunner open-source which was later re-named as Selenium Core. The Same Origin Policy Issue Same Origin policy prohibits JavaScript code from accessing elements from a domain that is different from it was launched. Example, the HTML code in www.google.com uses a JavaScript program “randomScript.js”. The same origin policy will only allow randomScript.js to access pages within google.com such as google.com/mail, google.com/login, or google.com/signup. However, it cannot access pages from different sites such as yahoo.com/search or guru99.com because they belong to different domains. This is the reason why prior to Selenium RC, testers needed to install local copies of both Selenium Core (a JavaScript program) and the web server containing the web application being tested so they would belong to the same domain of Selenium Remote Control (Selenium RC) Unfortunately; testers using Selenium Core had to install the whole application under test and the web server on their own local computers because of the restrictions imposed by the same origin policy. So another ThoughtWork’s engineer, Paul Hammant, decided to create a server that will act as an HTTP proxy to “trick” the browser into believing that Selenium Core and the web application being tested come from the same domain. This system became known as the Selenium Remote Control or Selenium 1.
Selenese commands can have up to a maximum of two parameters: target and Parameters are not required all the time. It depends on how many the command will need. For a complete reference of Selenese commands, click 3 Types of Commands Actions These are commands that directly interact with page elements. Example: the “click” command is an action because you directly interact with the element you are clicking at. The “type” command is also an action because you are putting values into a text box, and the text box shows them to you in return. There is a two-way interaction between you and the text box. Accessors They are commands that allow you to store values to a variable. Example: the “storeTitle” command is an accessor because it only “reads” the page title and saves it in a variable. It does not interact with any element on the page. Assertions They are commands that verify if a certain condition is met. 3 Types of Assertions Assert. When an “assert” command the test is stopped immediately. Verify. When a “verify” command fails, Selenium IDE logs this failure and continues with the test execution. WaitFor. Before proceeding to the next command, “waitFor” commands will first wait for a certain condition to become true. If the condition becomes true within the waiting period, the step passes. If the condition does not become true, the step fails. Failure is logged, and test execution proceeds to the next command. By default, timeout value is set to 30 seconds. You can change this in the Selenium IDE Options dialog under the General tab. Assert vs. Verify
HTMLUnit (none) WebDriver can drive HTMLUnit without the need of a driver server Firefox (none) WebDriver can drive Firefox without the need of a driver server Internet Explorer Internet Explorer Driver Server Available in 32 and 64-bit versions. Use the version that corresponds to the architecture of your IE Chrome ChromeDriver Though its name is just “ChromeDriver”, it is in fact a Driver Server, not just a driver. The current version can support versions higher than Chrome v.21 Opera OperaDriver Though its name is just “OperaDriver”, it is in fact a Driver Server, not just a driver. PhantomJS GhostDriver PhantomJS is another headless browser just like HTMLUnit. Safari SafariDriver Though its name is just “SafariDriver”, it is in fact a Driver Server, not just a driver. You can download these driver servers Summary Aside from a browser, you will need the following to start using WebDriver Java Development Kit (JDK). Eclipse IDE - Java Client Driver - When starting a WebDriver project in Eclipse, do not forget to import the Java Client Driver files onto your project. These files will constitute your Selenium Library. HTMLUnit and Firefox are the only browsers that you can automate without the use of a Driver Server. Each other browser has its own driver server. WebDriver Script Using the Java class “myclass” that we created in the previous tutorial, let us try to create a WebDriver script that would:
based on the CSS By.id by the value of their “id” findElement(By.id(“someId”)) finds a link by the exact text it findElement(By.linkText(“REGISTRATION”)) By.name by the value of the findElement(By.name(“someName”)) findElement(By.partialLinkText(“REG”))
There are times when we need to access elements (usually texts) that are within HTML tables. However, it is very seldom for a web designer to provide an id or name attribute to a certain cell in the table. Therefore, we cannot use the usual methods such as “By.id()”, “By.name()”, or “By.cssSelector()”. In this case, the most reliable option is to access them using the “By.xpath()” method. XPath Syntax Consider the HTML code below. We will use XPath to get the inner text of the cell containing the text “fourth cell”. Step 1 – Set the Parent Element (table) XPath locators in WebDriver always start with a double forward slash “//” and then followed by the parent element. Since we are dealing with tables, the parent element should always be the <table> tag. The first portion of our XPath locator should therefore start with “//table”. Step 2 – Add the child elements The element immediately under <table> is <tbody> so we can say that <tbody> is the “child” of <table>. And also, <table> is the “parent” of <tbody>. All child elements in XPath are placed to the right of their parent element, separated with one forward slash “/” like the code shown below. Step 3 – Add Predicates The <tbody> element contains two <tr> tags. We can now say that these two <tr> tags are “children” of <tbody>. Consequently, we can say that <tbody> is the parent of both the <tr> elements. Another thing we can conclude is that the two <tr> elements are siblings. Siblings refer to child elements having the same parent. To get to the <td> we wish to access (the one with the text “fourth cell”), we must first access the second <tr> and not the first. If we simply write “//table/tbody/tr”, then we will be accessing the first <tr> tag. So, how do we access the second <tr> then? The answer to this is to use Predicates.
keyDown(modifier_key) Performs a modifier key Does not release the modifier key - subsequent interactions may assume it's kept pressed. Parameters: modifier_key – any of the modifier keys (Keys.ALT, Keys.SHIFT, or Keys.CONTROL) keyUp(modifier _key) Performs a key release. Parameters: modifier_key – any of the modifier keys (Keys.ALT, or Keys.CONTROL) moveByOffset(x-offset, y-offset) Moves the mouse from its current position (or 0,0) by the given offset. Parameters: x-offset- horizontal offset. A negative value means moving the mouse left. y-offset- vertical offset. A negative value means moving the mouse up. moveToElement(toElement) the mouse to the middle of the element. Parameters: toElement- element to move to. release() Releases the depressed left mouse button at the current mouse location sendKeys(onElement, charsequence) a series of keystrokes onto the element. Parameters: onElement - element that will receive the keystrokes, usually a text field charsequence – any string value representing the sequence of keystrokes to be sent In the following example, we shall use the moveToElement() method to mouse-over on one Mercury Tours’ table rows. See the example below.
07.import org.openqa.selenium.firefox.FirefoxDriver; 08. 09. 10. 11.import org.openqa.selenium.interactions.Action; 12. 13.import org.openqa.selenium.interactions.Actions; 14. 15. 16. 17.public class myclass { 18. 19. 20. 21.public static void main(String[] args) { 22. 23.String baseUrl = ""; 24. 25.WebDriver driver = new FirefoxDriver(); 26. 27. 28. 29.driver.get(baseUrl); 30. 31.WebElement link_Home = driver.findElement(By.linkText("Home"));
Click on “Advanced System Settings”. Click on the “Environment Variables”. Step 3 In the Environment Variables dialog box, in the “System variables” section, scroll down the list until you find “Path” and then double-click it. Step 4 In the “Variable text box, add the new path “C:\Wget\”. Just be sure that there is a separating this new entry from the preexisting values. Step 5 Click OK on all dialog boxes. Launch the command prompt and type the command “wget”. You should get the following response. WebDriver and Wget In the following example, we will use WebDriver and wget to download a popular chat software called Yahoo Messenger. Our base URL shall be . Step 1 Import the “java.io.IOException” package because we will have to catch an IOException later in Step 4. Step 2