package test; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; public class testing { public static void main(String[] args) throws InterruptedException { // Download the geckodriver based on the firefox if it is 32 bit or 64 // bit installed in the machine // Save the driver and provide path of the exe for geckodriver System.setProperty("webdriver.gecko.driver", "E:\\Selenium\\geckodriver-v0.18.0-win64\\geckodriver.exe"); // define the capabilities of firefox browser with marionette set as true DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette", true); // create an instance of firefox driver WebDriver driver = new FirefoxDriver(capabilities); driver.get("http://www.rediff.com"); driver.quit(); } }
Showing posts with label POM Selenium concepts. Show all posts
Showing posts with label POM Selenium concepts. Show all posts
Sunday, July 23, 2017
Code to open firefox with selenium 3.4 using GeckoDriver 1.8
Thursday, July 20, 2017
Job creation in Jenkins: Workflow
Below are the steps to create a new job in Jenkins:
1. Launch Jenkins by providing the url and port number of Jenkins Server.
2. Click on New Item to create a new Jenkins Job. Provide the jenkins Job Name and select the project type. Let us select a freestyle project.
5. General Section:Job Description is provided in this section. Managing different build Information and adding Parameters used in job are some of the tasks which can be defined in this section.
6. Source Code Management: This section provide instruction on how to extract latest code in the workspace, from git, svn or local system.
7. Build Trigger : This section define how the job can be triggered, Whether it is dependent on existing job or is scheduled to run on a fixed time and date periodically.
Build Environment: This section manages the build environment or workspace with option to clean the workflow before build execution or abort/fail a build if it is struck at a step for long period.
Build - This is the main section where the actual batch files/ shell command or ant targets are invoked. For testing purpose , we can invoke a maven project in this section. Executing a job means executing the commands provided in the build section.
Post Build Steps - These are post build steps and include activities like archiving artifact or sending a mail with build details or publishing testng/junit test results.
Build Environment: This section manages the build environment or workspace with option to clean the workflow before build execution or abort/fail a build if it is struck at a step for long period.
Monday, May 15, 2017
Jenkins - Adding a conditional build step.
In Jenkins, at times we may require Jenkins to execute some build step on one condition and another in case of other condition. For e.g: It may be required to run a particular test/batch command on Weekdays and some other batch condition on weekends. We can create a job to perform different tasks based on the condition satisfied.
To add a conditional build step, follow below steps in Jenkins.
To add a conditional build step, follow below steps in Jenkins.
- Navigate to Jenkins>Manage Jenkins>Manage Plugins and go to available tab.
- Search for Conditional buildstep as shown below and click on install without restart.
- It will install the plugin and will show success once installation completes.
- Now the plugin can be used in the Jenkins job as shown below.In the job, add build step as Conditional step (single)
- Provide the conditional step from the list as shown below and execute the build command.
Saturday, April 22, 2017
Switching between multiple frames in selenium
There can be multiple iframes in a webPage. To perform action on webelements in a frame, we need to first identify the frame and then perform action on the element.We can switch to a particular frame based on the below identifiers:
By Index: The index depends on the order of frame in the Page. A numeric value is assigned to the frame based on the order in which it appears in the page.
driver.SwitchTo().Frame(0);
By Name: We can switch to a frame based on the name of the frame.
driver.SwitchTo().Frame("FrameName");
By Id: We can switch to a frame based on the id defined for the frame
driver.SwitchTo().Frame("FrameId");
Switch to a frame by locator for WebElement. In the below example frameElement is the locator for the frame
driver.switchTo().frame(frameElement);
e.g:
driver.SwitchTo().Frame(driver.FindElement(By.CssSelector(cssfortheelement)));
Frame inside frames: There can be scenario in which there are frame inside of the parent frame.Please note in case of frame inside frame, we need to first switch to the outer frame, and then switch to the inner frame to perform action on the webelement.
To acess the main webPage and come out of the frame, below code is used
driver.SwitchTo().DefaultContent()
Thursday, November 17, 2016
Log file for Selenium IDE execution using FileLogging addon for firefox
We can create log files of selenium IDE test execution and store in an file using the below process:
1. Download the File Logging add-on to Firefox as shown below.
2. Firefox will restart once the add-on is installed.
3. Now Open Selenium IDE in Firefox.
4. Go to Options>Options.
5. Selenium IDE options window will open.
6. Go to FileLogging tab.
7. Provide location of file where logs needs to be created.
8. Define the logging level.
9. Select the required checkbox for logging information.
Give it a try, it is quite useful.
Tuesday, June 14, 2016
Understanding Locators in Selenium WebDriver and common element types
Locators are used to identify an element in the WebPage. An object can be identified based on the attributes values. In this article, we will discuss the different locators based on which an element can be identified in the page using selenium webdriver.
Locators are used to locate an element in the Page based on the unique address of the object in the Page. It can be unique attribute value, for e.g: id of the element or the absolute or relative xpath of the element in the Page.
Once an object is identified in the Page, we can perform the required operation/action on the object. Below are the different common types of elements on the Page:
Common Elements Types
| ||
Type
|
Common Operations on Object Type
| |
Anchor elements or links
|
Clicking on a link
Validating links in Page are correct or broken
Validating links exist in the Page
| |
TextBox
|
Validating text box is available/enabled
Input data in the edit box
| |
Radio Button
|
Selecting a radio button
| |
Checkbox
|
Validating whether checkbox is selected or not.
Selecting/deselecting checkbox
| |
DropDown
|
Selecting value from the dropdown
| |
Labels
|
Extracting value from the label or validating label exist in the page.
| |
Table
|
Extracting information from table.
| |
Multi-Select ComboBox
|
Selecting multiple values from a combobox.
|
The different types of locators used to locate elements in selenium are described below:
Locator Types | ||
Locator | Definition | Usage |
ID | element matching "id" attribute of the element. Normally id of an element in the Page is expected to be unique. Hence if available, using id of the element is good option. | WebElement usr = driver.findElement(By.id("name")); |
Name | element matching "name" attribute of the element. There may be multiple element with same name in Page. In such scenario, first element matching the name is selected. | WebElement usr = driver.findElement(By.name("uname")); |
Link Text Partial Link text | For links in the Page, we can use linktext and partialLinkText to identify the links based on the name/Partial text of the link. Applicable for links element only | WebElement elem=driver.findElement(By.linkText("mail")); driver.findElement(By.partialLinkText("mail")); |
CSS Selector | Using CSS locators, we can identify elements. Both Xpath and CSS are useful and highly used in identifying elements in the Page.Absolute or relative Xpath can be created to identify element in the Page | WebElement elem = driver.findElement(By.cssSelector("a.test")); |
XPath | Please refer to my previous post for details on xpath and css: http://seleniumbites.blogspot.in/2016/01/xpath-and-css-for-selenium-webdriver.html | WebElement elem = driver.findElement(By.xpath("//input[@id='mail']")); |
ClassName | ClassName uses the class attribute of the element to locate the element. Foe e.g.: in case of identifying the random error message displayed in the Page, usually the message may have the same class attribute. We can create a collection of elements matching the class and gather the required error messages in the Page | driver.findElement(By.className("errormessage")); |
TagName | This locator is useful while working with list of elements in the Page. E.g: Finding number of links in the Page | List <WebElement> linklst = driver.findElements(By.tagName(“a”)); |
Friday, May 20, 2016
Saturday, March 12, 2016
Page Object Model pattern in Selenium?
Page Object Model Pattern serves following purposes:
- Segregating objects definition from test scripts in separate classes making test scripts independent of page elements and locators, thus reducing the maintenance cost in case of changes in elements locator in the Application under test.
- Defining Page elements and page methods in Page classes help in re-usability of the page elements and methods in different tests reduces the line of code and makes it more maintainable as changes in application needs to be implemented at Page classes only.
- Make code more readable in the test scripts.
- Easier for testers with less knowledge of selenium/Java to create test scripts once the Page classes are prepared.
Understanding how to automate a web application using selenium Web Driver using Page Object Model design pattern
Let us try to understand how Page object model pattern is implemented with an example.
Suppose there are number of pages in an Web Application and within each page there are multiple elements in each page. Let us assume linear approach for creating automated test cases. Suppose there are ~100 test scripts(workflows) to test the application. Each of the workflow starts with login into application. In each of the test cases, we would have defined the locators for input fields for username and password and login button elements and interact(perform action) with the element.
Creating test using above approach will result in following problems:
If you had worked with QTP before, the object repository used to be maintained in tree structure. In Page Object model design pattern,Web Pages are represented by classes with elements in the Page defined as members of the class. Actions on the elements are implemented as methods in the class very much similar to QTP approach.
- In case of changes in an element locator, changes needs to be implemented at multiple tests
- Readability of test will be poor due to locators defined at test level.
- Lines of code will be higher and approach for creating test scripts will vary from one test developer to another.
- Requires script developer to have good selenium knowledge for test scripts creation.
A better approach would be to define elements locators at a different location from test scripts. Also methods for working with elements should be defined, separate of the test scripts.
If you had worked with QTP before, the object repository used to be maintained in tree structure. In Page Object model design pattern,Web Pages are represented by classes with elements in the Page defined as members of the class. Actions on the elements are implemented as methods in the class very much similar to QTP approach.
Implementing Page Object Model:
- Create a class for login Page (similarly create class for different pages).
- Define the objects locator in the Page as member of the class.
- In the constructor method for the Page class, load or intialise the elements defined in the Page using Page Object Factory.
- Create user defined methods in the class to input data in the input box, clicking the login button, and methods for successful login or failure.
- An object of the page class will be created in the test scripts to interact with the objects and methods for Page class.
I have not added any code snippet in this article, but below reference article have nice examples explaining the code and concept of Page Object model pattern in selenium.
Useful References for code and concept of Page Object Model
- https://www.toptal.com/selenium/test-automation-in-selenium-using-page-object-model-and-page-factory
- https://www.youtube.com/watch?v=a0cTWcOfRtY&list=PL6tu16kXT9PqKSouJUV6sRVgmcKs-VCqo&index=8 - Actually all the videos of this channel (executeAutomation) are highly informative and very useful to start automation with selenium Webdriver.
Hope you find this post informative or atleast the references useful.
Subscribe to:
Posts (Atom)