Skip to main content

Posts

Showing posts from 2014

How to find a WebElement

Find Element & Find Elements Method The difference between “ Find Elemen t” and “ Find Elements ” method is the first returns a WebElement object otherwise it throws an exception and the latter returns a list of WebElements, it can return an empty list if no DOM elements match the query. The “Find” methods take a locator or query object called “ By ”. “ By ” methods are listed below. By ID With this strategy, the first element with the id attribute value matching the location will be returned. If no element has a matching id attribute, a  NoSuchElementException   will be raised. This is the most efficient and preferred way to locate an element, as most of the times IDs are unique. But in some cases UI developers make it having non-unique ids on a page or auto-generating the id, in both cases it should be avoided. Example : If an element is given like this: <input id=”username”></input> WebElement we=driver.findElement(By.id(“username”));  ...

Selenium Grid - Distributed Systems

Selenium grid is powerful and can be easily used with just few lines of code. A Grid is a network of computers which can be heterogeneous and geographically dispersed. A Selenium Grid is how a grid of computers are connected using Selenium and configured to perform a task. A task typically here is running a suite of test cases with different browsers in different platforms across geographically dispersed systems. The test cases can be run in parallel using Selenium. So if we have a grid of three computers the same set of test cases can be run in IE, Firefox and Chrome. To establish this we have three important components. 1) Hub 2) Node 3) Test Script A Hub acts like the server to which requests are sent . The Hub sends to request to various registered nodes. The HUB can be instantiated using command line by using the selenium server standalone jar file. Open command prompt, navigate to the folder which has the selenium server file and use this command. java -jar selen...

Selenium Wait

The different WebDriver Wait statements that can be useful for an effective scripting are listed below. It is a bad practice to use Thread.sleep() command because it makes the script slow and doesn't work with unpredictable environments. ImplicitlyWait Command Purpose: We can tell Selenium that we would like it to wait for a certain amount of time before throwing an exception that it cannot find the element on the page. We should note that implicit waits will be in place for the entire time the browser is open. This means that any search for elements on the page could take the time the implicit wait is set for. WebDriver driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://url_that_delays_loading"); WebElement myDynamicElement = driver.findElement(By.id("myDynamicElement")); FluentWait Command Purpose: Each FluentWait instance defines the maximum amount of time to wait for a condition,...

Form - User Input Automation

Text boxes WebElement t=driver.findElement(By.id(“username”)); t.sendkeys(“test name”); Buttons WebElement t=driver.findElement(By.id(“createbutton”)); t.click(); DropDown Boxes  WebElement select = driver.findElement(By.tagName("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 oSelection = new Select(driver.findElement(By.tagName("select"))); oSelection.deselectAll(); //All op...

Selenium - Data Driven Testing

In Automation, we need to runs scripts to test the same functionality with different types of data. This data is usually stored in an excel file and accessed via the script to enter in the Application Under Test. In QTP, this is done using the DataTable .  In Selenium we can use the Apache POI API to do read an excel workbook and write the output to the excel file. Using the API is quite simple if you understand the basics. POI  - Poor Obfuscation Implementation. This name was humorously given  because Microsoft code was deliberately made difficult to reverse engineer but still it was reverse engineered. POIFS - Poor Obfuscation Implementation File System. Used to access the input file. HSSF - Horrible Spreadsheet Format This is used to read and write .xls files XSSF - XML Spreadsheet Format This is used to read and write .xlsx files. FileInputStream fs=new FileInputStream("C:\\Users\\Krishna\\selenium workspace\\Data\\firstone.xls"); POIFSFileSyst...

Taking a screenshot for evidence

Taking a screenshot is very easy in selenium. It is supported in almost all the web browser drivers. The driver instance is casted into a TakesScreenshot type and a file can be obtained. The returned file is saved in the temporary folder with a alphanumeric name. The file can be copied to the results folder using FileUtils class. and renamed to what we want. In the below example it is renamed to Error with time appended to it. File screenshot =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(screenshot, new File("C:\\Users\\Krishna\\Desktop\\Error" + (new Date().getTime()) +".png")); RemoteWebDriver is used to run tests in remote browsers. They can also be used in the local system. The advantage of remotewebdriver class is the exceptions often have an attached screenshot encoded as BASE64 png. Most of the exceptions are implementing the Screenshot exception. The code to use this feature is.  public String extractScreenSho...

Logging using Apache Log4j

Introduction - Log4j is a brilliant logging API available both on Java and .net framework. - Log4j allows you to have a very good logging infrastructure without putting in any efforts. - Log4j gives you the ability to categorize logs at different levels (Trace, Debug, Info, Warn, Error and Fatal). - Log4j gives you the ability to direct logs to different outputs. For e.g. to a file, Console or a Database. - Log4j gives you the ability to define the format of output logs. - Log4j gives you the ability to write Asynchronous logs which helps to increase the performance of the application. - Loggers in Log4j follow a class hierarchy which may come handy to your applications Log4j consists of five main components - LogManager - Loggers - Appenders - Layouts - Configuration file   Log Manager This is the static class that helps us get loggers with different names and hierarchy. You can consider LogManager as a factory producing logger objects. package Log4jSam...

Transition from Test Automation Engineer to Test Engineer

What's the difference? Test Automation Engineer works on automating the Regression Test Suite. Test Engineer ensures the product can be released to the customer. With my industry experience, I have realized, that, as part of automation engineer your job duties don't stop with creating Automation tests, Frameworks and running them. With more tests being automated, your responsibilities not only include running the complete Regression suite but occasionally validation and verification of the product. There are different documents/sites defining different types of testing and where their usage, however,but finding bugs is a different skill. Though we are testers by role, our analytical minds work like developers as we develop scripts too. We could be biased. When something doesn't work, we look for a workaround and think it's no big deal. This attitude is a deal breaker!Instead use your skills to analyse and break the code. Here I am going to list some of my ideas ...