Please log down y our all questions on any topic which arise in your mind while taking over the trainning process
33 Comments
Last 5 and 6 November 2011 was the Saigon MDC 2011(http://www.mobiledevcamp-vn.org/?lang=en). Over I took a training session on android webdriver automation test training session. That training's session slides are attached here. If you missed that training session then also I hope this slide will help your learning invention . Cheers !
Its pretty important. try the following, you will be able to do this !
public class WebDriverTestClass { public static void main(String a[]) throws InterruptedException { try { WebDriver driver = new FirefoxDriver(); // Go to Google Home Page26 driver.get("http://www.google.com"); // Look for search textbox and enter search term there WebElement searchBox = driver.findElement(By.name("q")); searchBox.sendKeys("WebDriver API"); // Click on 'Search'33 WebElement searchButton = driver.findElement(By.name("btnG")); searchButton.click(); // Not required or recommended any where, but just wait for the last37 // click() // operation to get completed fine Thread.sleep(2000); // to return the url System.out.println("What's the current Url: "+ driver.getCurrentUrl()); // if you wish to take screenshot of this page, File scrFile = ((TakesScreenshot) driver) .getScreenshotAs(OutputType.FILE); FileUtils.copyFile(scrFile, new File( "c:\\screenshot\\googlesearch-webdriverapi.png")); // Close the driver, once you're done driver.close(); } catch (Exception e) {53 e.printStackTrace(); // For debugging purposes54 } } } 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: Test Automation on Mobile handsets and other related devices has largely been believed as ineffective and inefficient. The primary and foremost reason being that the difficulties which the automation scripts and tools had to deal with because different hardware characteristics , the resolution settings on the device, the language settings and the other hardware constraints. We have come up with a Mobile Test automation framework which is going to generate instant ROI for automation of functional test cases. The framework broadly covers the following high level approach:
Today I am going to share a very interesting topic with you. Android phone, what do you think when every I tell in front of you? Interesting or some even can tell me “no this is quite boring”. But if in over all we examine that then this is quite popular among the people. In north American countries iphone is dominating there is a little use of android. But if we consider in Asia then android is the king of this area. Because a lot of people are using and really become a fan of this. So whenever something being popular the depend software development rate also increase. So now days a lot of android app is developing all over the Asia. Even those software companies which are from West they are also investing in android app developing. • So whenever we are having a lot of android software developing then we have to perform the test operation on them too to ensure the quality. Right? Unlucky there were no tools for real test automation in android even in 2009 and 2010. Really surprising but true. Now we are in the pipeline of android test automation tools. Let’s think now how people normally test a mobile or android app? • You come up with a test plan based on the features of the application. • You take into account the requirements of the platform. • For Android, we're talking about stability, responsiveness, accessibility, performance, elegance, security, and so on. • it varies by company as it always has, but the fundamentals are the same. It requires a budget, personnel, hardware, and will. That is something like that. Previously when we use to do android test automation then we need the following materials to perform the test automation. Such as: · Android main project · Test project · IDE · Some tools even need the permission inside the mailfest.xml file. Now here is the point is. Many project is not develop in one company and submit to another company to test. So now your client how can belive in you that you will not steal the code from that project? Haha. Maybe you are thinking, “What nonsense are you saying? I never think so bad way!” But this is ture that maybe you are not thinking but many other people are there to think quite much about this. Even if I ask you how can you trust other people with your project. This was pretty hard for both side. So android test automation test was not speed up. Because people can’t trust each other. So they just project apk and confirm manual testing. But I belive that there is no limitation is not a real limitation. If we effort then we can overcome that too. So now totally I am going to tell you one new technology where you don’t need any project just apk which is enough to do test automation. So lets list which things are need for this automation • Android environment • IDE • Application.apk You must be thinking how this is possible. Right? Soon I will post a video demo on this. The most significant benefits of traditional test automation are the decreased test execution time and cost, and increased test coverage for each testing cycle. The downside of test automation is the increased test development cost. In a typical test automation scheme, each test is first documented by a test engineer then scripted by an automation engineer. The cost benefit of automated testing comes through the repeated execution of the test automation suite. Since the cost of executing an automated test is far less than the cost of executing a manual test, eventually the decreased test execution cost will make up for the increased development costs.
For example, suppose that a test engineer costs your organization $60/hr, and an automation engineer costs your organization $100/hr. The test engineer can create 5 new test cases per hour during test plan development, and can execute 10 test cases per hour during test execution. The automation engineer can develop 2 test cases per hour. Once those test cases are completed, a test engineer can execute and analyze the results of 50 tests in one hour (typically the test engineer will run the tests overnight, or while he is doing some other work, then analyze the results once the test is done). Suppose you create a test plan with 1000 test cases: For manual testing, your initial test development cost is: 1000 tests * 1 hour/5 test cases * $60/hour = $12,000 The cost of executing all of the tests manually is: 1000 tests * 1 hour/10 test cases * $60/hour = $6,000 For automated testing, your initial cost is: $12,000 + 1000 tests * 1 hour/2 test cases * $100/hour = $65,000 The cost of executing all of the test cases w/ test automation is: 1000 tests * 1 hour/50 test cases * $60/hour = $600 The cost benefit of test automation is realized by running the tests several times (i.e. with each weekly or nightly build of your application). In this case, we begin to see a cost savings if we run the tests more than 12 times. There are several reasons why people choose to automate tests: o Speed up testing to accelerate releases o Allow testing to happen more frequently o Reduce costs of testing by reducing manual labor o Improve test coverage o Ensure consistency o Improve the reliability of testing o Allow testing to be done by staff with less skill o Define the testing process and reduce dependence on the few who know it o Make testing more interesting o Develop programming skills So here we go with the top challenges: 1) Testing the complete application: Is it possible? I think impossible. There are millions of test combinations. It’s not possible to test each and every combination both in manual as well as in automation testing. If you try all these combinations you will never ship the product 2) Misunderstanding of company processes: Some times you just don’t pay proper attention what the company-defined processes are and these are for what purposes. There are some myths in testers that they should only go with company processes even these processes are not applicable for their current testing scenario. This results in incomplete and inappropriate application testing. 3) Relationship with developers: Big challenge. Requires very skilled tester to handle this relation positively and even by completing the work in testers way. There are simply hundreds of excuses developers or testers can make when they are not agree with some points. For this tester also requiresgood communication, troubleshooting and analyzing skill. 4) Regression testing: When project goes on expanding the regression testing work simply becomes uncontrolled. Pressure to handle the current functionality changes, previous working functionality checks and bug tracking. 5) Lack of skilled testers: I will call this as ‘wrong management decision’ while selecting or training testers for their project task in hand. These unskilled fellows may add more chaos than simplifying the testing work. This results into incomplete, insufficient and ad-hoc testing throughout thetesting life cycle. 6) Testing always under time constraint: Hey tester, we want to ship this product by this weekend, are you ready for completion? When this order comes from boss, tester simply focuses on task completion and not on the test coverage and quality of work. There is huge list of tasks that you need to complete within specified time. This includes writing, executing, automating and reviewing the test cases. 7) Which tests to execute first? If you are facing the challenge stated in point no 6, then how will you take decision which test cases should be executed and with what priority? Which tests are important over others? This requires good experience to work under pressure. 8 ) Understanding the requirements: Some times testers are responsible for communicating with customers for understanding the requirements. What if tester fails to understand the requirements? Will he be able to test the application properly? Definitely No! Testers require good listening and understanding capabilities. 9) Automation testing: Many sub challenges – Should automate the testing work? Till what level automation should be done? Do you have sufficient and skilled resources for automation? Is time permissible for automating the test cases? Decision of automation or manual testing will need to address the pros and cons of each process. 10) Decision to stop the testing: When to stop testing? Very difficult decision. Requires core judgment of testing processes and importance of each process. Also requires ‘on the fly’ decision ability. Over to you: Many of you are working in manual and/or automation testing field. Though I have addressed many of above challenges in our previous articles, I want your views on handling these software testing challenges. Feel free to express your views in comment section below. Do you have interest in glassfish Admin GUI with selenium and experience the differences between silk testt and Selenium tools
1. Run Selenium from the build If you build admin-gui module you can find the README.SeleniumRC file under the ../glassfish/admin-gui/tests directory. Just follow the instruction from the file, you should be able to setup and run Selenium JBI tests with Glassfish Admin GUI. If you encounter a failure from your browser, open selenium test case files under java directory and replace statement selenium.waitForCondition("Selenium.browserbot.isNewPageLoaded()", TIMEOUT_PERIOD) by selenium.waitForPageToLoad( TIMEOUT_PERIOD) 2. Run Selenium with Glassfish V2 binary bit 1. You must setup the environment by installing Selenium IDE on your Firefox browser 2. Having ant 1.6.5 and jdk1.5.0_11 or latest version installed on your system 3. Create a AdminGUITest directory on your system and copy the following 4 files to the ../AdminGUITest/lib directory selenium-server.jar , selenium-java-client.jar, selenium-java-client-driver-tests.jar and testng-5.5-jdk15.jar andjunit.jar 4. Copy the simple test harness (AdminGUITest.jar file) to your AdminGUI directory and unjar the file 5. Update build.properties file with your setup environment 6. Start the selenium remote server (assume you're using a window system) cd %AdminGUITest.home% java -jar selenium-server.jar 7. Start glassfish domain1 8. Run the first AdminGUI test case cd %AdminGUITest.home% ant run-test 3. Differences between SilkTest and Selenium Navigating
The first thing you'll want to do with WebDriver is navigate to a page. The normal way to do this is by calling "get": driver.get("http://www.google.com"); WebDriver will wait until the page has fully loaded (that is, the "onload" event has fired) before returning control to your test or script. Interacting With the Page Just being able to go to places isn't terribly useful. What we'd really like to do is to interact with the pages, or, more specifically, the HTML elements within a page. First of all, we need to find one. WebDriver offers a number of ways of finding elements. For example, given an element defined as: <input name="passwd" id="passwd-id" /> we could use any of: WebElement element; element = driver.findElement(By.id("passwd-id")); element = driver.findElement(By.name("passwd")); element = driver.findElement(By.xpath("//input[@id='passwd-id']")); 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 "setSelected" 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.getValue())); option.setSelected(); } This will find the first "SELECT" element on the page, and cycle through each of it's OPTIONs in turn, printing out their values, and selecting each in turn. As you can see, this isn't the most efficient way of dealing with SELECT elements. WebDriver's support classes come with one called "Select", which provides useful methods for interacting with these. 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: element.submit() Getting Visual Information And Drag And DropSometimes you want to extract some visual information out of an element, perhaps to see if it's visible or where it is on screen. You can find out this information by casting the element to a RenderedWebElement: WebElement plain = driver.findElement(By.name("q")); RenderedWebElement element = (RenderedWebElement) plain; Not all drivers render their content to the screen (such as the HtmlUnitDriver), so it's not safe to assume that the cast will work, but if it does you can gather additional information such as the size and location of the element. In addition, you can use drag and drop, either moving an element by a certain amount, or on to another element: RenderedWebElement element = (RenderedWebElement) driver.findElement(By.name("source")); RenderedWebElement target = (RenderedWebElement) driver.findElement(By.name("target")); element.dragAndDropOn(target); Moving Between Windows and FramesIt's rare for a modern web application not to have any frames or to be constrained to a single window. 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 chaining switchTo() calls, and you can specify the frame by its index too. That is: driver.switchTo().frame("frameName") .switchTo().frame(0) .switchTo().frame("child"); would go to the frame named "child" of the first subframe of the frame called "frameName". All frames are evaluated as if from currently switched to frame. To get back to the top level, call: driver.switchTo().defaultContent(); There are several steps required to prepare the android device or emulator, the environment and the computer. These include: · Installing android-server.apk (the actual filename may differ e.g. if you use a prebuilt version it will include the release name). If you want to use an emulator, we currently recommend using one with Android 2.2 installed. The server doesn’t support earlier versions any more (it used to but limitations with the platform e.g. lack of support for XPath meant the project team have chosen to target a minimum of 2.2). There’s currently an issue with the Java-to-JavaScript bridge in version 2.3 of the emulator, so again we don’t recommend using it currently.) · Configuring port forwarding so your tests can communicate with the device or running instance of the emulator. · Starting the WebDriver Server on the Android emulator (or on a device). Configuring your eclipse project You will need to include the various jar files from the selenium project in order for your tests to use ‘RemoteWebdriver’. The AndroidDriver implements the RemoteWebdriver protocol. You can download a zip file from the selenium project site http://code.google.com/p/selenium/downloads/list e.g. selenium-java-2.0b1.zip currently. This file needs to be unzipped, which will unpack to 2 selenium jar files (the code and the javadoc documentation) and a series of 27 other jar files. All these files need to be added to the build path in your eclipse project. I suggest copying all the files into a common libs folder for your project. First, let's create an Android Virtual Device (AVD): $cd ~/android_sdk/tools/ $./android create avd -n my_android -t 12 -c 100M -n: specifies the name of the AVD. -t: specifies the platform target. For an exhaustive list of targets, run: ./android list targets Make sure the target level you selected corresponds to a supported SDK platform. -c: specifies the SD card storage space. When prompted "Do you wish to create a custom hardware profile [no]" enter "no". Now, start the emulator. This can take a while, but take a look at the FAQ below for tips on how to improve performance of the emulator. $./emulator -avd my_android & Install the WebDriver APKEvery device or emulator has a serial ID. Run this command to get the serial ID of the device or emulator you want to use: $~/android_sdk/platform-tools/adb devices Download the Android server from selenium download page To install the application do: $./adb -s <serialId> -e install -r android-server.apk Make sure you are allowing installation of application not coming from Android Market. Go to Settings -> Applications, and check "Unknown Sources". Start the Android WebDriver application through the UI of the device or by running this command: $./adb -s <serialId> shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity You can start the application in debug mode, which has more verbose logs by doing: $./adb -s <serialId> shell am start -a android.intent.action.MAIN -n org.openqa.selenium.android.app/.MainActivity -e debug true Sample Code: just put that inside a class and check the magic, Run the Testsimport junit.framework.TestCase; import org.openqa.selenium.By; import org.openqa.selenium.WebElement; import org.openqa.selenium.android.AndroidDriver; public class OneTest extends TestCase { public void testGoogle() throws Exception { WebDriver driver = new AndroidDriver(); // And now use this to visit Google driver.get("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()); driver.quit(); } }
still confused ??? haha. Really no problem. Check my video tutorial on android webdriver. You will have your all questions answer. |
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
|