copy paste

How Do You Calculate Your ROI On Test Automation With Selenium?

Written by  on October 15, 2019

Posted by Arnab Roy Chowdhury | June 19, 2019 Automation • Cross Browser Testing • Selenium Tutorial •

 

Cross browser testing has been a type of testing which requires a tremendous amount of effort and time. The process of testing your web-app over different browsers, operating systems, devices, screen resolutions to evaluate the rendering of your web content for a variety of your audience is an activity. Especially, if approached manually. Automated cross browser testing with Selenium can help you save the time of routine test activities, helping you cut short on regression testing. However, people seldom like changes. If manual testing is popular in your organization, the management will obviously raise questions when you ask them to implement test automation.

Test automation although highly beneficial may often prove to be expensive, but is it worth the cost? That is one question which you may find yourself struggling with while convincing the higher management. You will be required to present an effective ROI on test automation with Selenium while developing a web application, highlighting the benefits of automation testing with Selenium for automated cross browser testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation) of a web application by making it faster with less manual labor.

ROI

In this article, we shall discuss different metrics to evaluate ROI on test automation with Selenium, and techniques for calculating ROI covering the basics as well as advanced techniques.

Metrics For Evaluating ROI On Test Automation With Selenium

You as well as your team members can consider certain metrics and measurements which will help you analyze the ROI on test automation with Selenium as you plan to start automation testing from scratch of your web application. These metrics could differ from one organization to another. Why? Well, it is a matter of prioritization, there are different metrics such as the number of defects detected, time gain or test coverage directly influence risk, cost, quality, and delivery schedule of your project. Some organizations may prioritize the number of defects detected, as they may believe, that quantity will lead to quality. Some may do vice-versa, as for them quality means everything. What’s your take? What do you think is more crucial in the debate of quality vs quantity of test cases. Let me know your thoughts in the comment section below.

With that said, identifying key metrics to calculate ROI on test automation with Selenium is critical for you, before you step into the discussion with higher management.

giphy

Scope Of Test Automation With Selenium

We know we can’t perform a 100% test automation. Well, how much-automated cross browser testing can your perform is a question that needs a lot of thought process? If you wish to perform automated cross browser testing for your web-app then you would have to think and prioritize which browsers, & OS should you be covering in your test cases? Because you can’t cover every scenario. The total number of possible scenarios could lead to hundreds or even thousands of test cases. If your automation test script is as long as that then it may take you quite a considerable amount of time on a daily basis evaluation of your web application or website.

In short, this is where you need to compare between the total count of automated test cases vs the total count of test cases where you can implement automation.

In case you wish to reduce time on complex test suites then you can also go for parallel testing with Selenium Grid ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/speed-up-automated-parallel-testing-in-selenium-with-testng/). That way, you can execute multiple test scripts, simultaneously. However, for that as well you may need to think about how many concurrent or parallel sessions would be good enough for your requirements? You can do so by our concurrency calculator ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/concurrency-calculator/).

Improvement in this metric signifies that the teams can find the defect faster and fix them quickly, thus leading to low risk and high ROI on test automation with Selenium.

How Much Time Would You Save?

With Agile, there are weekly or biweekly deliverables along with frequent changes in requirements. The importance of regression testing in that scenario, increases. Implementing automated regression test cases will reduce the time required for testing, thereby, gaining more time to invest in development or working on another sprint. Saving time is a priority for almost every organization, especially startups, who need to scale their web-application quickly. Is time one of your concerns for evaluating ROI on test automation?

Bandwidth Of Your Resources

We know that test automation with Selenium will help you to market your web-application quickly. However, no organization would prefer if their employees sit idle for the majority of their time, waiting for their script to complete. Calculating ROI on test automation with Selenium requires a thorough effort analysis of every automation & manual tester you have got on board.

Investment Budget Over Resources & Tools

Test automation sure saves time and effort. However, it involves a tradeoff with pricing. You need to think about how much budget you can comfortably allocate over expenses around tools, every organization, especially startups, who need to scale their web-application quickly. Is time one of your concerns for evaluating ROI on test automation?

Total Defect Count

The total defect count after each regression cycle gets completed gives an indication of the product quality and how much effective automation testing is for the specific project.

Figuring out the True ROI for Automation Testing

Based on the number of regression cycles that need to be executed during the project’s lifetime, the true ROI can shift to a positive zone. ROI is usually calculated by the following formula:
ROI = (Manual Testing cost – Automated testing cost)/Automated testing cost

But the classic method is not valid anymore with agile and DevOps coming in the market. Also, this metric is not realistic since the count of manual tests can never be the same as automated tests. For calculating the true value of ROI of test automation with Selenium automation on the basis of quantity is not an option preferred by many. But it is also not entirely neglected.

Defect Quality

This, in my opinion, is a very important metric for calculating ROI on test automation with Selenium. I believe, that the whole point of test automation with Selenium is to not erase the need for manual testers in the project. The point of automation testing is to reduce the amount of time consumption from a tester’s narrow schedule, so they could come up with more out of the box test cases. Improving the quality of your test case will definitely help you shape your web-application for better.

General Mistakes While Calculating ROI On Test Automation

Although calculating ROI involves basic calculation using some simple formula, mistakes may occur if you miss some important parameters. Let’s discuss some common mistakes which people make while calculating ROI.

ROI On Test Automation

Src: https://www.cartoonstock.com/directory/r/return_on_investment.asp ^(http://seoblackhatforum.com/goto/https://www.cartoonstock.com/directory/r/return_on_investment.asp)

You Really Didn’t Ignore Manual Testing Completely…Did You?

Only keeping automated testing efforts as the primary measuring parameter is one of the biggest mistakes. Manual testing will always be important. When it comes to cross browser testing, some scenarios can be automated but there are areas where you would need to interact with your web-app live, by performing manual cross browser testing. Because visual defects are easily detected manually than running an automation script. Facts like whether the website looks good in all browsers or whether a certain navigation menu works properly in a specific browser are always checked manually. If you automate these tests, they will not provide a high ROI on test automation with Selenium. Even if you don’t calculate the manual efforts, you still have to spend time and money on it.

Always Think About the Bigger Picture

While measuring the ROI on test automation with Selenium, you must consider a longer time period. Inspecting how a certain testing methodology will benefit the organization for a short time is not ideal. You must check how it impacts the organization as well as the team in the long run. Instead of months, calculate how the impact will be over a period of 3 to 5 years. For instance, should you opt for shift-left testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-shift-left-testing-can-help-your-product-quality/)? Shift left testing is a methodology which focuses you to perform testing as soon as possible, right from the requirement gathering phases. The idea behind is to think of bugs and find them as soon as possible because it is believed that a bug found at a later stage of SDLC will be a lot more expensive compared to a bug which is found in the initial stages.

Did You Sync Your Organization’s Capability?

You must sync the capabilities of your organization along with the test automation tool ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation) stack. For successfully implementing an automation testing strategy, both product knowledge, as well as knowledge in automation, is required. Your team should have a clear understanding of how to use the planned automation tool as well as how the application works.

Test Maintenance Is An Important Factor To Consider

Maintenance of the test cases is another factor people tend to miss while measuring ROI on test automation with Selenium. When you are using Selenium for automated cross browser testing then after successful implementation of a testing strategy, you will regularly need to update and maintain the test cases. The regression suite and your test cases will start to grow as you add new pages, enhancement or update the features of your web application. Ensuring the usability of those test cases for a longer time period will require regular maintenance.

Missing Out On Proper Documentation

This is a very common mistake, and it is not only from an automation tester perspective but also from a managerial point of view. Documentation should be set as a standard for every organization. When an automation tester writes a test script, they should prepare a document explaining the purpose of that script and the working of it. A common knowledge base should be made available to collect document around every automation script that is active for your organization. This would serve as a foundation for every budding resource who gets on-board with the process. It would also help to eliminate any collateral damage that your web-application may receive by the absence of any senior test automation engineer or if an automation tester went to switch from your company to another. So the next time you calculate your ROI on test automation with Selenium, make sure to keep in mind the effort & bandwidth involved in maintaining one such knowledge base for internal employees.

Action Items For Maximum ROI On Test Automation With Selenium

By far we realize the common mistakes, metrics for calculating ROI on test automation with Selenium. What comes next? The execution part. What is the best way to get the maximum ROI on test automation with Selenium? Well, here are a few noteworthy actionable insights to help you derive the maximum profit from test automation.

ROI

Implement Automation For New Test Cases

This is a very important factor to consider, especially if you are switching from manual to automation. Let’s suppose you are introducing Selenium WebDriver for automated cross browser testing in your organization.

  • Calculate the number of test cases which are required to be automated
    • In this step, check which ones should be automated and which ones should remain manual.
    • Don’t convert every test case for automation. There are some things which should only be tested manually.
  • Calculate the hourly cost of the testers who are executing your test cases.
  • If some testers have no experience in automation, calculate the training cost as well.

Prioritize The Process Of Automating New Test Cases

We all know that regression testing is always kept at top priority, especially when it comes to visual regression testing of a web application across different browsers to check its cross browser compatibility ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/feature).

Regression testing mostly involves repetitive execution of the old test cases to ensure that some newly added functionalities or enhancements have not introduced any new or old defect. Over time, when your web apps grow in architecture as well as functionality, keeping the process of regression testing manual will prove to be costly. Implementing automated visual regression testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/smart-visual-ui-testing) makes sense if you want to reduce the cost.

  • While calculating the ROI, assume each new test case will soon become a part of regression testing. Keep them as a part of your regression test strategy.
  • Sort test cases according to complexity and check which can be automated among them.
  • As said before, keep into account the cost of maintaining the old test cases.

Nearabout 100% Test Coverage Across Different Test Configurations Of Browsers & OS

The primary goal of automation testing is to improve the quality of your application. While calculating ROI, you should also consider the fact that the mode of viewing a website is increasing daily. There are hundreds of browsers and devices in the market on which people may view your web application and the number is growing regularly. Define a browser compatibility testing matrix ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/creating-browser-compatibility-matrix-for-testing-workflow/).

Best Practices to Increase the Coverage Rate

You can increase the rate of environmental coverage by executing smoke tests, unit tests, regression tests, noting the defect leakage.

Unit tests – Unit tests cover the highest count while running the testing phase of your web application. It always makes sense when you invest in a parallel testing mechanism to save time.

Smoke tests – Running smoke tests in parallel is the best way to cover the test cases while pushing a hotfix into the application. Automated smoke tests are a great way to evaluate your web-application on a daily basis.

Regression Tests – In today’s Agile age, rapid deployment demands more and more regression testing to test version control. Running parallel regression tests ensures that each latest build works perfectly like the previous build, helping you to expand the test coverage in a significantly shorter amount of time.

Keep in mind the Defect leakage – It is the number of bugs that occur during the production cycle because they were not detected during the previous testing phases. These can occur due to less functional testing coverage or a poor testing environment.

Try using the shift left testing approach. This involves testers validating the application before it is developed. Developers are also involved in it by running unit test cases once they finish developing a certain module. The core idea is to start uncovering bugs sooner which will ultimately reduce the cost.

Figure Out The Count Of Reusable & Redundant Test Cases

Duplicate test cases are an important factor that may lead to the growing budget for testing. There is no point in recreating the same test cases which you used earlier for a different module. Reusing test cases leads to improvised testing speed and a faster test cycle.

Calculating this cost involves checking

  • The number of repeated test cases
  • Test cases having duplicate components
  • The time required for detecting and developing all those redundant test cases.
  • Calculate the cost of using a test case management tool

Best Practises to Reduce Redundancy

  • Use a Test case management tool to find out duplicate scripts. You can use those tools to store the tests with custom fields which can be personalized later according to your requirements. Using a test case management tool will help you to search for redundancies quickly.
  • You can also develop modular test scripts which can be reused later. Find out the tests which are executed more often. For example, sign in our sign out functionality. For checking if these 2 works perfectly, you have to test across a number of variations. Create a modular test case which can be used for every login and log out variations.

Execute Hassle-Free Automated Cross Browser Testing With A Selenium Grid On-Cloud

An imperative area for calculating ROI on test automation with Selenium lies around the execution method. Selenium, as we know, is an open-source test automation framework meant for facilitating web-app testing. Now, you can perform automation testing with Selenium either locally on your own or by using one of the cloud-based tools which offer Selenium Grid.

When you are performing automation testing with Selenium through your own infrastructure then you would have to keep in mind the budget when you scale your automation testing suite. How will you introduce new devices? New browser versions? Your existing machine would also need a heavy hardware upgrade for supporting Selenium Grid’s parallel execution. However, if you are performing test automation with Selenium Grid on the cloud ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation) then you can scale effortlessly along with your project requirements.

Selenium on its own doesn’t provide a feature for test reporting. You can extract test reports using test automation frameworks based upon the language you use. If you are using cloud-based Selenium Grid of LambdaTest then you can extract these reports through our Open Selenium API ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/lambdatest-launches-api-for-selenium-automation/).

Another key difference between the two methods lies around parallel testing. With a Selenium Grid defined on your local machine, you will only be able to run your test cases on browsers which are installed in that local machine. However, if you are using a cloud-based Selenium Grid such as LambdaTest then you get to test across 2000+ real browsers & browser versions.

LambdaTest is a cross browser testing cloud where you can run multiple Selenium test cases across multiple browser-device combinations in parallel, reducing your cost involved for preparing an in-house lab for cross device and cross browser testing. ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/landing-text-cta)

Remember, without properly executed parallel testing, there remains the risk of encountering defects later in the testing cycle. The faster you detect a bug, the less costly it is to fix it.

Advanced ROI Calculation Techniques

Now that we have covered the basics, let’s get to know the advanced techniques used for calculating ROI.

Efficiency ROI

Src: https://www.cartoonstock.com/directory/r/return_on_investment.asp ^(http://seoblackhatforum.com/goto/https://www.cartoonstock.com/directory/r/return_on_investment.asp)
Since automation test cases can run all day long, ROI calculation is performed in terms of days. On the other hand for manual testing, only the working hours of a tester is counted, which is on an average, 8 hours. The formula, which forms the base of calculating ROI is

(a) Automation test script development time = (Hourly automation time per testNumber of automation test cases)/8
(b) Automation test script execution time = (Automation test execution time per test
Number of automation test casesPeriod of ROI)/18
(c) Automation test analysis time = (Test Analysis time
Period of ROI)/8
(d) Automation test maintenance time = (Maintenance timePeriod of ROI)/8
(e) Manual Execution Time = (Manual test execution time
Number of manual test cases*Period of ROI)/8

Note: Period of ROI is the number of weeks for which ROI is to be calculated, Divided by 8 is done wherever manual effort is needed. Divided by 18 is done wherever automation is done.

In Efficiency calculation, the primary focus is on how much effective automation testing is to the organization. The money factor is considered secondary and it is not mandatory to include the tester’s hourly billing rate.

Risk Reduction ROI

This involves independently calculating the benefits of automation. We will again take the example of cross-browser testing using WebDriver to understand how it works. During manual testing, the entire testing team used to spend a lot of time running the same test cases repeatedly across multiple browsers. After the introduction of automation, they have a lot of extra time in hand for performing productive work like designing test cases, analyzing the application, etc. In short, Risk reduction ROI addresses the issues which were not addressed previously.

With the implementation of automation, test coverage increases. Depending entirely on manual testing will lead to unnecessary bugs which may occur after delivery. Thus leading to reduced product quality as well as testing efficiency. This probable loss is considered to be a risk. There is no change in investment cost. Only the monetary loss is calculated which the organization may face without the implementation of automation.

It’s A Wrap!

Thanks for the read, I hope you now have sound knowledge & strategic clarity to help convince your higher management around the ROI on test automation with Selenium. Don’t make the mistake of thinking that ROI is just about comparing the time and money spent while testing a web application manually versus automating the test cases. There are a lot of factors to consider, each coming with their own benefits and pitfalls. Also, ROI cannot be calculated using a single formula. There are multiple mechanisms and what you need to choose depends on your organization’s requirements and the project’s situation. Happy testing! 🙂

Common Challenges In Selenium Automation & How To Fix Them?

Written by  on October 15, 2019

Posted by Himanshu Sheth | May 22, 2019 Automation • Cross Browser Testing • Selenium Tutorial •

 

Selenium is one of the most popular test frameworks which is used to automate user actions on the product under test. ​Selenium is open source and the core component of the selenium framework is Selenium WebDriver. Selenium WebDriver allows you to execute test across different browsers like Chrome, Firefox, Internet Explorer, Microsoft Edge, etc. The primary advantage of using the Selenium WebDriver is that it supports different programming languages like .Net, Java, C#, PHP, Python, etc. You can refer to articles on selenium WebDriver architecture to know more about it.

Though Selenium makes a website or web-app testing simple, there are a fair number of challenges in Selenium automation that developers face while using the framework. Let’s have a look at some of the most common challenges faced In Selenium Automation, along with their resolution.

False Positives & False Negatives(Flaky Tests)

False positive is a scenario where your test result comes out to be successful, even though when it is not. Vice-versa, False negative is a scenario where the test results report an error with a script execution, even though everything is working as intended. False positives & False negatives have always posed a challenge for web automated testing, and Selenium is no different.

When you are running hundreds or thousands of test cases through your Selenium script then there may be a chance where you encounter such flaky tests which show false positives or false negatives. If left unhandled for long, flaky tests may end up leaving a tester with a deluded image of their automation test scripts or web application under test.

Flaky tests are certainly one of the most common challenges in Selenium automation. They could be tricky to manage, and I would love to elaborate some ways through which you can deal with test flakiness, but that would be a detailed discussion in itself. I will be covering an entire article around handling flaky tests soon. Stay tuned for that by hitting the notification bell.

Waiting For Web Page With JavaScript To Load

When your website contains dependent web element, such as drop-down lists based on the user selection then Selenium scripts at run-time may behave abruptly around these web elements. This may happen because your WebDriver didn’t process the time taken for the webpage to load entirely. To handle these common challenges in Selenium automation around page loading, you may need to make your WebDriver wait until the complete JavaScript for that page is loaded. Before you perform a test on any webpage, you should make sure that loading of the webpage (especially those with a lot of JavaScript code) is complete. You can make use of readyState property which describes the loading state of a document/webpage. document.readyState status as ‘complete’ indicates that the parsing of the page/document is complete & all the resources required for the page are downloaded.

Not So Scalable Approach

Selenium is a great automation testing tool, and being open-source it has helped make life easier for web testers around the world. However, one of the major challenges in Selenium automation is the inability to scale.

The ultimate goal of performing automation testing is to cover more test coverage in less time. Initially, you may have short test builds, however, your product is bound to expand with every sprint. This would mean you may need to cover a larger number of test cases. Using Selenium WebDriver, you could only perform testing in a sequential way and that won’t be as effective as you may want your automation process to be. Also, the speed at which tests will be executed would depend upon your computing speed.

Now, we know that is where a Selenium Grid comes to aid by empowering you to run test cases in parallel. But there is a downside to that as well. You cannot thoroughly test your website or web-app across multiple combinations of browsers + OS. Why? Because your Selenium Grid would only help to execute cross browser testing on the specific browsers that are installed in your local machine.

LambdaTest ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/) provides a cloud-based Selenium Grid ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation) to help you paddle faster through your release cycles. You can test your web-app or website on more than 2000 real browsers, browser versions and operating systems on the cloud.

Instead of linear testing, you can leverage the power of Parallel testing in Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/parallel-testing-in-selenium-webdriver-with-python-using-unittest/), through which you can reduce overall project costs, accelerate product/feature delivery as the automation tests are executed in Parallel (i.e. concurrent sessions).

Handling Dynamic Content

A website or web application may consist of static content or content that is dynamic in nature. Testing a website or a web application that has static content poses a minimal number of challenges in Selenium automation. However, in today’s times, most of the websites contain content that that may vary from one visitor to another. That implies that the content is dynamic in nature (AJAX based apps).

For example, an e-commerce website can load different products based on the location from where the user has logged in or content may vary depending on what the user has selected from a particular drop-down menu. As the new content takes time to load, it is important to fire your test only when the loading is complete. Since elements on a web-page are loaded at different intervals, there might be issues if an element is not yet present in the DOM. This is why handling dynamic content has been one of the most common challenges in Selenium automation ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation).

One easy solution to solve this problem is putting the thread to sleep for a couple of seconds which may give sufficient time to load the content. However, this is not considered a good practice, because, irrespective of whether the required event occurs or not, the thread would sleep for that much time duration.

A better approach to handle this challenge with Selenium WebDriver dynamic content would be to use Implicit Wait or Explicit Wait (depending on your requirement).

Explicit Wait For Handling Dynamic Content

Using an explicit wait, you can make the Selenium WebDriver halt the execution & wait till a certain condition is met. It can be applied along with thread.sleep() function if you wish to set a condition to wait until an exact time period. There is a number of ways in which explicit wait can be accomplished, WebDriver with ExpectedCondition is the most popular option.

In the example above, two searches are performed on the URL under test https://www.lambdatest.com ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/). The first search is for the element home-btn where an element is located via CLASS_NAME for a maximum duration of 10 seconds. Check out our blog, if you are not aware of how to use Locators in Selenium WebDriver ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/locators-in-selenium-webdriver-with-examples/).

The second search is for a clickable element login for a maximum duration of 10 seconds. If the clickable element is present, a click() action is performed. In both cases, WebDriverWait is used along with ExpectedCondition. 500 milliseconds is set as the default limit for which the ExpectedCondition gets triggered by the WebDriverWait, until a successful response is received.

Some of the common Expected Conditions are below:

  • title_is
  • title_contains
  • presence_of_element_located
  • visibility_of
  • invisibility_of_element_located
  • presence_of_all_elements_located
  • text_to_be_present_in_element
  • text_to_be_present_in_element_value
  • frame_to_be_available_and_switch_to_it
  • element_to_be_clickable
  • staleness_of
  • element_to_be_selected
  • element_selection_state_to_be
  • element_located_selection_state_to_be
  • alert_is_present
  • element_located_to_be_selected

Implicit Wait For Handling Dynamic Content

Implicit wait informs the WebDriver to poll the DOM for a certain time duration in order to get the presence of web element(s) on the page. The default timeout is 0 seconds. The Implicit Wait requires an effort of a one-time setup, when configured properly, it would be made available for the lifetime of the Selenium WebDriver object.

In the above example, the web element home-btn-2 is not present on the URL under test i.e. https://www.lambdatest.com ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/) and search times out after timeout-value of 60 seconds.
Below is the output of the test execution:

output of the test execution

Handling Pop-up Windows

There could be scenarios where you need to automate interaction with pop-up windows, it is another one of the most common challenges in Selenium automation. There are different types of pop-up windows –

  1. Simple alert – something that displays some message.
  2. Confirmation alert – requests the user for confirmation of operation.
  3. Prompt Alert – informs the user to input something.

Though Windows-based alerts cannot be handled by Selenium WebDriver, it does have the capability to handle a web-based alert. The switch_to method is used to handle pop-ups.
For demonstration, we create a simple HTML file which contains an Alert popup implementation.

Below is the usage of switch_to method where we do .switch_to.alert.alert in order to switch to the alert dialog box. Once you are at the Alert Box, you can accept the alert by using the alert.accept() method.

In case a page displays input alert, you can make use of send_keys() method to send text to the input box.

Switching Browser Windows

Multi-Tab testing is certainly one of the common challenges in Selenium automation. An ideal scenario is the one where the button click opens a pop-up window which becomes the child window. Once the activity on the activity on the child window is complete, the control should be handed over to the parent window. This can be achieved using switch_to.window() method where window_handle is passed as the input argument.

As shown in the example below, the parent window opens the link: http://www.quackit.com/html/codes/html_popup_window_code.cfm ^(http://seoblackhatforum.com/goto/http://www.quackit.com/html/codes/html_popup_window_code.cfm)

Once the link is clicked, a new pop-window is opened which becomes a child window. switch_to.window method is used to switch back to the parent window. There is an option to use driver.switch_to.window(window-handle-id) or driver.switch_to.default_content() to switch to the parent window.

You Can’t Test For Mobile Devices

Though the Selenium framework is widely used for testing websites or web-apps across different combinations of browsers & operating systems, the testing is still limited to non-mobile devices. Thus, testing your website or web-app for mobile devices comes as one of the great challenges in Selenium automation

If you wish to perform test automation for mobile applications then the most renowned open-source framework would be Appium.

If you are looking to test your website on different mobile devices and screen resolutions then LambdaTest can help you do that manually, as of now. You can capture screenshot of the bug, highlight the bug with an in-built image editor, send it across to various third-party tools such as JIRA, asana, slack, Trello, & more. You could even record videos of your test session with in-built screen recorder facility available on LambdaTest.

After a successful launch of Selenium on our platform ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/lambdatest-now-live-with-an-online-selenium-grid-for-automated-cross-browser-testing/), We are now working on incorporating automation for testing on mobile devices using Appium which would very handy for accelerating your testing activity. Stay tuned & if you wish to enquire on updates around Appium then feel free to give our 24/7 customer chat support a shout. You could also mail us at support@lambdatest.com.

You Can’t Automate Everything

100% automation is a myth. It is a known fact that not all the test scenarios can be automated since there would be some tests that would require manual intervention. You need to prioritize the amount of effort that your team should spend on automation testing vis-à-vis manual testing. Though Selenium framework has features through which you can take screenshots, record video (of the overall execution of the tests) and other aspects of visual testing, using those features with a scalable cloud-based cross browser testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/) platform like LambdaTest can be a big value-add to your overall testing.

You can have a look at our insightful blog to save time on manual cross browser testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/34-ways-to-save-time-on-manual-cross-browser-testing/).

Generating Test Reports

The primary intent of any testing activity is to locate bugs and improve the overall product. Reports can play a major role in keeping a track of the tests being executed, generated output, and results of the tests. Though there are modules like pytest_html (for Python) that can be used along with pytest and Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/test-automation-using-pytest-and-selenium-webdriver/), the information in the test report may not be very exhaustive. There are similar types of modules/packages for different programming languages such as Java, C#, .Net, etc. that can be used with Selenium, but the same problem persists for these languages too. Gathering test reports is a one of the critical challenges in Selenium automation.

LambdaTest provides you with a scalable testing infrastructure to perform automated cross browser testing using the Selenium Grid hosted on our cloud servers. LambdaTest also launched API for Selenium Automation ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/lambdatest-launches-api-for-selenium-automation/) to help you extract meaningful test data of the execution of your script on LambdaTest platform to your preferred onsite instance of cloud storage. Using LambdaTest API you can do the following:

  • Extract and manage test information
  • Retrieve build information such as build test status, individual test status, test run time, errors, and test logs
  • Get command by command screenshots
  • Detailed information about the available browser environments on LambdaTest platform and more.

LambdaTest API helps you to extract, and analyze your Selenium test reports with as granular detail as you want. ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/lambdatest-launches-api-for-selenium-automation/)

Conclusion

Mentioned above are some of the common challenges in Selenium automation, there are some limitations as far as Selenium is concerned. You can only use the Selenium framework to test web applications i.e. it cannot be used to test local windows based applications. There are some scenarios where you may want to by CAPTCHA or RECAPTCHA which is used to prevent any kind of bots submitting information in the page. Since CAPTCHA is for security purpose, you can never bypass that authentication.

In scenarios where you would like to use Selenium to test your web application across different browsers, operating systems, and devices in a more scalable manner, you can make use of LambdatTst. You can port your code which makes use of Local Selenium Web Driver to LambdaTest’s Remote WebDriver with minimal effort.

These were some of the most common challenges in Selenium automation in my opinion. What’s yours? If you have got any questions or experience around implementing Selenium automation, then feel free to let us know in the comment section below. Happy testing. 🙂

Complete Guide For Using XPath In Selenium With Examples

Written by  on October 15, 2019

Posted by Sadhvi Singh | May 14, 2019 Selenium Locators • Selenium Tutorial •

 

Identifying element has always been the trickiest part and therefore require an accurate and correct approach. If you are unable to find elements by the easily available approaches like ID, class, name, link or tagname then XPath in Selenium can help rescue you. Locating dynamic elements have always been the pain area while you wish to automate the scripts, the only ray of hope to deal with such fiascos is XPath. In my current article, I will be digging into the steps of locating an element via XPath example in Selenium and its various ways. We will be looking into the below sections:

  1. What Is XPath in Selenium? ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid1)
  2. Types Of XPath in Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2)
    1. 2.1   Absolute ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid1.1)
    2. 2.2  Relative ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid1.2)
  3. Writing Dynamic XPath in Selenium by different ways: ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid3)
    • 3.1  Basic XPath ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.1)
    • 3.2  Using ‘OR’ & ‘AND’ ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.2)
    • 3.3  Using Contains() ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.3)
    • 3.4  Using Starts-With ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.4)
    • 3.5  Using Text() ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.5)
    • 3.6  Using Index ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.6)
    • 3.7  Using Chained XPath ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.7)
    • 3.8  XPath Axes ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8)
      • 3.8.1   Following ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.1)
        1. 3.8.1.1  Following Sibling ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.1.1)
      • 3.8.2   Preceding ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.2)
        1. 3.8.2.1  Preceding Sibling ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.2.1)
      • 3.8.3  Child ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.3)
      • 3.8.4  Parent ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.4)
      • 3.8.5  Descendants ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.5)
      • 3.8.6  Ancestors ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/#testid2.8.6)

If you wish to look into other approaches of locating an element, you can view the below articles:

  1. How to locate element via ID ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/)
  2. How to locate element via Name ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/)
  3. How to locate element by Tagname ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/locating-elements-by-tagname-in-selenium/)
  4. How to locate element by CSS Selectors ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/)
  5. Different locators in Selenium WebDriver ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/locators-in-selenium-webdriver-with-examples/)

So, What Is XPath In Selenium?

XPath known as the XML path is a language that helps to query the XML documents. It consists of expression for a path along with certain conditions to locate a particular element.

So let’s see how to write an XPath in Selenium. Below is the DOM structure for LambdaTest Registration Page ^(http://seoblackhatforum.com/goto/https://accounts.lambdatest.com/register/):

Referenced screenshot of the page:

Referenced screenshot

LambdaTest is a cross browser testing cloud that offers a Selenium Grid of 2000+ real browsers & browser versions to help you deliver a seamless user experience. Click here to get started for free. ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/landing-text-cta)

In order to visualize the above XML document, I have created the below flowchart.

flowchart

Here, I will write the XPath in Selenium from the root node which is the form tag in our case, then will locate the Full name field using div 2 child and the attribute value of the full name field. The XPath created in this case will be as below:

//form/div[@class=’ col-sm-12 google-sign-form’]/input[@name=’ name’]

In this case, I have referenced one attribute of the children and grandchildren in order to differentiate from the multiple children and grandchildren of each parent. From this we can derive the general syntax of XPath as follows:

//tagname[@attribute name= ‘value’]

Here

  1. // Denotes the current node
  2. Tagname : Define the tagname you are referencing to locate the element.
  3. Attribute Value : The attribute of the define tag through which you wish to narrow down the search.
  4. Value: Represents the value of any chosen attribute.

Nowadays, thorough use of multiple plugins and tools can help one find the XPath in Selenium with relative ease. The reason I don’t encourage people to do so, the XPath provided by these tools sometimes turn out to be brittle. Also, it may lead to people forgetting the basic concepts of creating XPath locators. Using firebug and Chrome Dev Tools one can also copy the required XPath in Selenium.

Types Of XPath In Selenium

There are two types of XPath

  1. Absolute XPath
  2. Relative XPath

Absolute XPath Example In Selenium

In case of absolute XPath in Selenium, the XPath expression is created using the selection from the root node. It starts with a single slash ‘/’ and traverses from the root to the whole DOM to reach to the desired element. The biggest disadvantage of using this as locating an element is, if during the course of development any changes made in the path, may lead to a failed XPath expression. For example:

/html/body/div[1]/section/div/div[2]/div/form/div[2]/input[3]

In the above XPath example in Selenium, if any tag name like section or one of the div’s changes the whole XPath would become invalid leading to script failure.

Relative XPath Example In Selenium

In case of relative XPath in Selenium, the XPath expression is generated from the middle of the DOM structure. It is represented by a double slash ‘//’ denoting the current node. In this case the search will start from the mentioned tagname and string value. It is more compact, easy to use and less prone to been broken. For example:

//input[@name=’email’]

In above XPath example in Selenium, we are searching from the current node with tagname input having attribute as name with value as email.

Below is a code snippet highlighting the XPath written using relative XPath for the register page of LambdaTest.

Writing Complex & Dynamic XPath In Selenium Through Various Methods

Using Basic XPath

This is the common and syntactical approach of writing the XPath in Selenium which is the combination of a tagname and attribute value. Here are few basic XPath examples in Selenium:

  • Xpath=//input [@name=’password’]
  • Xpath=//a [@href= ‘https://www.lambdatest.com/ ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/)’]
  • Xpath=//*[@id=’email_01’]
  • Xpath=//input[name=’email’][@placeholder=’Work Email’]

The first two examples in the basic XPath list seems self-explanatory. In it we have used tags as input and anchor tag with their corresponding attribute value. The last two examples are just extended version of using XPath in Selenium. In the third example we have just excluded the HTML tag and represented it with an asterisk(*). In this case, the script will search in the DOM with any HTML tag having an ID attribute with value as ‘email_01’. In case of example 4, we have created the XPath using multiple attributes for the single HTML tag.

Web UI Testing with Python and LambdaTest ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/webinar/web-ui-testing-with-python-and-lambdatest#bookyourseats)

Using ‘OR’ & ‘AND’

As mentioned these logical expressions are used on the attributes condition. In case of OR any one of the conditions should be true or both, whereas in the case of AND, both the conditions should be fulfilled. For example, for the below DOM structure, XPath using AND and OR can be written as:

  • Xpath= //input[@type= ‘email’ or @name= ‘email’]
  • Xpath= //input[@type= ‘email’ and @name= ‘email’]

In case of OR if any of the attribute value matches, the element will be located, whereas in case of AND both the attribute value should match and met, then only the element will be located. The above defined ways have been inculcated in the code snippet below, where we are trying to input values on the LambdaTest register page. ^(http://seoblackhatforum.com/goto/https://accounts.lambdatest.com/register)

Below is the console error showcasing as the last element not found as conditions were not met.

 error showcasing as the last element not found

Please Note: Both ‘and’ and ‘or’ should be case sensitive. In case you tend to use, ‘OR’ or ‘AND’, you will get an error in console stating invalid xpath expression

Using Contains()

This approach in creating XPath in Selenium comes handy when we have part of attribute values that changes dynamically. For example lets say the ID for the login field which is for instance email_01, have the ending number which keeps changing every time the page is loaded. In this case using contains helps us locate element with constant names like in this case ‘email’. Example for creating XPath with contains for the below DOM structure is:

  • Xpath=//*[contains (@placeholder, ‘Organization)]
  • Xpath= //input[contains (@name, ‘organization)]
  • Xpath=//*[contains(@class, ‘sign-up-input)]

Here in the above XPath example for Selenium we have used, attributes like placeholder, name etc and consider partial values to identify elements using contains keywords. Referenced below code snippet highlighting the usage of above XPath creation, in this we are clicking on the ‘Start testing’ button on LambdaTest homepage. Referenced screenshot:

LambdaTest homepage

Using Starts-With()

This is similar to the above contains method, the only difference is the comparison value here starts with an initial string value. This is useful when partly values changes for a given attribute. Like in the above XPath example, the email value changes for the latter values. Here, one can use starts-with approach to identify the element. Below XPath examples for Selenium, highlight the usage of starts-with for the below DOM structure:

  • Xpath=//input[starts-with(@placeholder, ‘Organization)]
  • Xpath= =//input[starts-with(@name, ‘organization)]

Here, in the example above we have used two attributes with the starts-with Keyword. The script will find for the tag name and matches with the attribute value that starts with ‘Organization’. Referenced code snippet highlighting the usage of Starts-With keyword while locating element via XPath in Selenium. We will use the same example as above, the only difference is, we will locate an element using starts-with.

Using Text()

This keyword is used to create expressions for XPath in Selenium when we have a text defined in an HTML tag and we wish to identify element via text. This comes really handy when the other attribute values change dynamically with no substantial part of the attribute value that can be used via Starts-with or Contains. Below is an XPath example in Selenium, highlighting the usage of text for the following DOM structure:

  • Xpath= //button[text()=’ Signup for Free’]
  • Xpath=//button[contains(text(),’ Signup’ )]

In the above mentioned examples, we have use the text on the button to identify the element. Here two instances of the examples are used, one where the text is exact matched, whereas the other where the text is matched partially using contains keyword. Referenced snippet below highlighting the usage of the text keyword. In this example, we will be clicking on one of the blogs on the LambdaTest blog ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/) page. Referenced screenshot below:

 LambdaTest blog

Using Index

This approach comes in use when you wish to specify a given tag name in terms of index value you wish to locate too. For instance, consider a DOM having multiple input tags for each different field value and you wish to input text into the 4th field. In this case you can use the index to switch to the given tag name. For example:

For the given DOM structure, I wish to locate the second input tag field:

  • Xpath= //div[@class= ’ col-sm-12 google-sign-form’]/input[2]

In the above example, the locator with ‘Full name’ as a field will be selected. The first input tag will be ignored whereas the other tag will be considered owing to the index mentioned. This type of approach comes in handy when dealing with data in tables. For example, when you have multiple rows, you can reference the desired row using an index. Reference code snippet using an index for a tabular form of data for the registration page of LambdaTest, where through the form class we are navigating to the ‘full name’ field using index.

lambdatest login page

Using Chained XPath In Selenium

As the name signifies, we can use multiple XPath expressions and chained them. For example, in the below DOM structure referencing the LambdaTest homepage links:

Complete Guide For Using XPath In Selenium With Examples

Here, we will try to navigate to the ‘Login link’. The syntax to achieve it is.

  • Xpath=//ul[@class=’nav navbar-nav navbar-right’]//li[@class=’sign-in’]

In the above XPath example in Selenium, I have located the parent class for the link tags and then navigated to the child using chaining for the ‘login’ link. The above example has been achieved using the below code snippet.

Console Output

XPath example in Selenium

XPath axes

XPath axes comes in handy when the exact element tagname or its attribute value are dynamic and cannot be used to locate an element. In this case locating element after traversing through child/sibling or parent will be easy approach. Some of the widely used XPath axes are:

1.   Following: This XPath axes helps to locate element following the current node. Mentioned below is the DOM structure of the LambdaTest Registration Page.

Referenced screenshot highlighting the two input elements, wherein we will try to locate the element ‘Full name’ field through the element ‘Organization’ field

Complete Guide For Using XPath In Selenium With Examples

  • Xpath=//input[@name=’ organization_name’]//following::input[1]
  • Xpath= //input[@name=’ organization_name’]//following::input

In the above-mentioned examples, the first one will select the input following the organization element, whereas in the second example, it will select all elements following the organization element having input tag. These are useful when we intent to locate element in a table or when we do not have any whereabouts of the following element from the current node. Mentioned below code snippet displays the elements located via following. In this example, we will use the Lambatest homepage, where we will traverse through the menu header options using the single header class. Referenced screenshot below:

Complete Guide For Using XPath In Selenium With Examples

2.   Following sibling: This is one concept that people tend to get confused with. All you get to clear yourself with this concept, is siblings. In case of following all nodes under the current node are the targeted ones irrespective they are under(children) the context node or not but follows below the context node. In case of following siblings, all following nodes of the context node, that shares the same parent, are applicable. In this case all siblings are referred to as children of the parent node. So if you are referencing one of the children and wish to navigate to other children of the same parent that follows it, following sibling does the business.

For example, using the LambdaTest homepage links, in the DOM structure below, one of the children has been referenced, and from it, we will navigate to its siblings.

In this case, the parent is,its children are the different li, here we will locate the sign in link and from their locate the login using following sibling

  • Xpath= //li[@class=’sign-in’]//following-sibling::li

Below is a code snippet incorporating the XPath example in Selenium.

Console Output

Complete Guide For Using XPath In Selenium With Examples

3.  Preceding:This method helps in locating element before the current node, as in the preceding element from the current node with XPath in Selenium. Below is a demonstration of the DOM structure.

Referenced screenshot below:

Complete Guide For Using XPath In Selenium With Examples

  • Xpath=//input[@name=’password’]//preceding::input[1]
  • Xpath=//input[@name=’password’]//preceding::input

In the above-mentioned example, the first one will locate element with the field as email whereas the other one will locate all elements before the current node i.e. password field. This is also useful in locating elements that cannot be located by any means and can be traversed through. For example, in the case of cross browser testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/), sometimes few elements cannot be recognized on IE browsers, legacy browser versions. In this case traversing to these elements using precedence or following could be helpful. Referenced example below with code snippet, highlighting how using the blog link on the LambdaTest homepage, we will circulate through the Automation tab ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation).

Complete Guide For Using XPath In Selenium With Examples

4.  Preceding-Sibling: This is a concept similar to following sibling, the only difference in functionality is that of preceding. So, in this case, you can switch among the siblings, but in this, you will switch from the context node been a child and move to the preceding node, you wish to locate. Both the child will share the same parent. Using the same example as mentioned in the following sibling, we will now move from the sign-up link to the login link using preceding-sibling. The syntax for the same is

  • Xpath= //li[@class=’login’]//preceding-sibling::li[1]

Note: In case you do not specify the index of the sibling you wish to navigate it may choose to select any. For specific selection, you need to mention the index. In case of a single sibling from the context node, you need not specify the index. Below is the code snippet for the same XPath example in Selenium.

Console Output

output

5.  Child: As the name specified this approach is used when we intent to locate all child elements of the current node. A basic use case of this approach could be when we wish to circulate through all the data in a table through the rows, in this case we can find all the child of a particular table. For example, using the below referenced DOM structure, we can create an XPath in Selenium as follows:

Referenced screenshot of the same.

Complete Guide For Using XPath In Selenium With Examples

  • Xpath= //div[@class=’ col-sm-12 google-sign-form’]/child::input
  • Xpath= //div[@class=’ col-sm-12 google-sign-form’]/child::input[1]

In the above-mentioned example, the first will locate all the input fields of the div for which are organization field, name field, email field, password field and phone number field, where as the other is used to locate element with the organization field only.

Below is the code snippet highlighting the use of child axes on the same. We are using the same register example of LambdaTest register page ^(http://seoblackhatforum.com/goto/https://accounts.lambdatest.com/register).

6.  Parent: This method is used to select the parent node of the current node. For example, for the below referenced DOM structure, the parent node is located through XPath.

Referenced screenshot:

Complete Guide For Using XPath In Selenium With Examples

  • Xpath= //input[@name=’email’]//parent::div

In the above example, the parent element is located through its child element. From the email field we are traversing to its parent node. Please note in case of child XPath, only the immediate children of the current node are considered and not the grandchildren. In case if the current node is the root node, then its parent will be empty and hence it will have no parent aces. Below is the code snippet for the same:

Console Output

Complete Guide For Using XPath In Selenium With Examples

7.  Descendants: This approach is used to locate element via XPath for all the children and sub children of the current node. For the below DOM structure, the descendants for XPath in Selenium are located as:

Referenced Screenshot:

  • Xpath= //div[@class=’ col-lg-3 col-md-4 col-sm-6 sign-form’]//descendant:: input

In the example above all the elements like email field, password field and button will be selected. One can also select one of the mentioned fields using index. Below is a code snippet highlighting the use of it.

Console Output

Complete Guide For Using XPath In Selenium With Examples

8.  Ancestors: In this method the context node, parent or its grandparents are selected via the Ancestors axes. For example, for the below DOM structure the ancestor will be defined as:

Referenced screenshot:
output

  • Xpath= //input[@name=’email]//ancestor::div[1]
  • Xpath= //input[@name=’email]//ancestor::form

In the example above, the first one will point to the parent node of the login fields where the other will point to its grandparent node which is the tag form in this case. Below code snippet highlighting the use of the same.

Console Output

 Console Output

Conclusion

XPath in Selenium helps an individual find out solutions in locating elements when the standard processes do not work. When to use which is important when it comes to different complexities of the DOM structure and the functional needs. As defined earlier in my article, cross browser testing could be one of the areas where you can explore the creation of XPath in Selenium, due to different browser behaviors towards DOM element. Make sure whichever method you opt in case of XPath are less prone to failure and make the scripts more robust, neater and easier to maintain. Cheers!

Complete Guide On TestNG Annotations For Selenium WebDriver

Written by  on October 15, 2019

Posted by Sadhvi Singh | April 24, 2019 Selenium Tutorial •

 

TestNG is a testing framework created by Cédric Beust and helps to cater a lot of our testing needs. It is widely used in Selenium. Wondering on what NG stands for? Well, it refers to ‘Next Generation’. TestNG is similar to Junit but is more powerful to it when it comes to controlling the execution flow of your program. As the nature of framework, we tend to make our tests more structured and provides better validation points through the use of TestNG.

Some of the noteworthy features of TestNG are:

  • Powerful and wide variety of annotations to support your test cases.
  • Helps to perform parallel testing, dependent method testing.
  • Flexibility of running your tests through multiple sets of data through TestNG.xml file or via data-provider concept.
  • Test cases can be grouped and prioritized as per need basis.
  • Provides access to HTML reports and can be customized through various plugins.
  • Test logs can be generated across tests.
  • Can be easily integrated with eclipse, Maven, Jenkins etc.

A basic process flow of a TestNG programs involves the following steps:

testNG programs involves

So, before jumping onto the annotations in TestNG for Selenium, it would be better to refer the prerequisites are required to setup TestNG.

Prerequisites

  • Java Development Kit
  • Setup Eclipse or any other IDE.
  • Install TestNG in Eclipse or any other IDE.

Note: Annotations can be used only with Java version 1.5 or higher.

If you are new to TestNG framework then follow our guide to run your first automation script with TestNG ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/a-complete-guide-for-your-first-testng-automation-script/).

So, What Is An Annotation?

An annotation is a tag that provides additional information about the class or method. It is represented by ‘@’ prefix. TestNG use these annotations to help in making a robust framework. Let us have a look at these annotations of TestNG for automation testing with Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation).

@Test

The most important annotation in TestNG framework where the main business logic resides. All functionalities to be automated are kept inside the @Test annotation method. It has various attributes based on which the method can be reformed and executed.

Example of a code snippet below validating the url :

@BeforeTest

This annotation is run before your first @Test annotation method in your class. You can use this annotation in TestNG for Selenium to setup your browser profile preferences, for example auto opening your browser in maximize mode, setting up your own customized profile for your browser etc.

Below is the code snippet for BeforeTest method ensuring the browser opens in maximize mode:

@AfterTest

This annotation in TestNG runs after all your test methods belonging to your class have run. This is a useful annotation which comes handy in terms of reporting your automation results to your stakeholders. You can use this annotation to generate report of your tests and share it to your stakeholders via email.

Example of the code snippet below:

@BeforeMethod

This annotation in TestNG runs before every @test annotated method. You can use it to check out for the database connections before executing your tests or lets say different functionality been tested in your @test annotated method which requires user login in a certain class. In this case also you can put your login code in the @BeforeMethod annotation method.

Below code snippet is an example, displaying login functionality of the LambdaTest platform:

@AfterMethod

This annotation runs after every @test annotated method. This annotation can be used to take screenshots of every test method ran against test runs .

Below code snippet indicating screenshot taken in the @AfterTest annotation in TestNG for Selenium:

@BeforeClass

This annotation runs before the first test method in the current class.This annotation can be used to setup your browser properties, initialize your driver, opening your browser with the desired URL etc.

Below is the code snippet for BeforeClass:

@AfterClass

This annotation runs after the last test method in the current class. This annotation in TestNG can be used to perform clean up activities during your tests like closing your driver etc

Below is the example of code snippet showing closing activities performed:

@BeforeSuite

A suite can consist of multiple classes, this annotation runs before all the tests methods of all the classes. This annotation marks the entry point of execution. @BeforeSuite annotation in TestNG can be used to perform the needed and generic functions like setting up and starting Selenium drivers or remote web drivers etc.

Example of @BeforeSuite annotation in TestNG, code snippet showcasing setting up of driver:

@AfterSuite

This annotation in TestNG runs post all the test methods of all the classes have run. This annotation can be used to clean up the processes before completing off your tests when you have multiple classes in functioning, for example closing the drivers etc.

Below is the code snippet for @AfterSuite annotation in TestNG for Selenium:

@BeforeGroups

TestNG helps testers create a bunch of tests into groups through the attribute group used in the @Test annotation. For example, if you wish all similar functionalities related to user management to be clubbed together, you can mark all tests like Dashboard, profile, transactions etc. into a single group as ‘user_management’. This @BeforeGroups annotation in TestNG helps to run the defined test first before the specified group. This annotation can be used if the group focuses on a single functionality like stated in the above example. The BeforeGroup annotation can contain the login feature which is required to run before any other methods like user dashboard, user profile etc.

Example of the Code snippet for @BeforeGroups annotation in TestNG for Selenium:

@AfterGroups

This annotation in TestNG runs after all the test methods of the specified group are executed.
Example of Code snippet for @AfterGroups annotation in TestNG for Selenium:

TestNG Report:

testngreport

Console Output:

console_output_1

 

Execution Sequence Of Annotations In TestNG For Selenium

All annotations described above are executed on runtime in the following order:

  • BeforeSuite
  • BeforeTest
  • BeforeClass
  • BeforeGroups
  • BeforeMethod
  • Test
  • AfterMethod
  • AfterGroups
  • AfterClass
  • AfterTest
  • AfterSuite

Here is an image of the basic workflow of these annotations:

console output

Attributes Used With Annotations In TestNG

These test annotations in TestNG have multiple attributes that can be used for our test method. The attributes further help in defining our tests and help in providing clarity in terms of execution flow of the different test’s method used in the TestNG class. Listing them below:

  • Description: It defines the test method. One can define what a method does via the description. For example, @Test(description=”this test validates the login functionality”).
  • alwaysRun: this attribute when used with a test method ensures it always run irrespective of the fact even if the parameters on which the method depends fails. When the value is set to true this method will always execute. For example, @Test(alwaysRun= true).
  • dataProvider: This attribute is set to provide data from the dataProvider annotated test to the test provided with this attribute. For example, let’s say you intent to run your tests on multiple cross browsers, where a dataProvider annotated test is written which contains multiple inputs of browsers and their corresponding versions. In this case the test containing this attribute will use those inputs of data to run you tests on multiple browsers. Syntax for the same is, @Test(dataProvider=”cross browser testing”).
  • dependsOnMethods: This attribute provides details to the execution flow, wherein the test is executed only if its dependent method mentioned in the attribute is executed. In case the test on which the method depends on is failed or not executed, the test is skipped from the execution. For example, @Test(dependsOnmethod=”Login”).
  • groups: This attribute helps to groups your test methods focusing onto a single functionality into one group. For example, @Test(groups=”Payment_Module”). This attribute also helps in the longer run when one can choose to ignore few groups during the execution cycle and chose over the other groups. All one need to do is mention the included groups in the TestNG.xml file within the include tag whereas the excluded groups can be defined using the exclude tag in the xml file.
  • dependsOnGroups: this attribute performs the above two attributes functions in collation i.e. it defines the test method with the attribute ‘dependsOn’ the defined groups. Once that group of tests are run, only post that this annotated method would execute. For example, @Test(dependsOnMethods = "Payment_Module" ).
  • priority: This attribute helps us to defined priority of the test’s methods. When TestNG executes the @Test annotated method, it may do so in random order. In a scenario where you wish that your @Test annotated method runs in a desired sequence you can use the priority attribute. The default priority of all test methods is 0. Priorities in ascending order are scheduled first for execution for example, @Test(priority=1), @Test(priority=2), in this case test with priority equal to one will be executed first then the test with priority as 2.
  • enabled: This attribute comes into picture, when you have an intent to ignore a particular test method and don’t want to execute it. All you need to do is set this attribute to false. For example, @Test(enabled= false).
  • timeout: This attribute helps to define the time a particular test should take to execute, in case it exceeds the time defined by the attribute, the test method would terminate and will fail with an exception marked as org.testng.internal.thread.ThreadTimeoutException. For example, @Test(timeOut= 500)Please note the time specified is in milliseconds.
  • InvocationCount: This attribute works exactly like the loop. Based on the attribute set across the test method, it would execute that method those number of times. For example, @Test(invocationCount = 5), this would execute the test 5 times.
  • InvocationTimeOut: this attribute is used in unison with the above invocationCount attribute. Based on the set value of this attribute along with the invocationCount, this ensures the test runs the number of times specified as per the invocationCount in the defined time set by the invocationTimeOut attribute. For example, @Test(invocationCount =5,invocationTimeOut = 20 ).
  • expectedExceptions: this attribute helps to handle the exception the test method is expected to throw. In case the one defined in the attribute is set and thrown by the test method it is passed else any other exception not stated in the attribute and thrown by the test method, would make the test method fail. For example, @Test(expectedExceptions = {ArithmeticException.class }).

The above defined are the attributes used with the annotations in TestNG for Selenium. Below is the code snippet showcasing the use of the above attributes:

Console Output:

console_output_2

TestNG Report:

TestNgReport

Annotations In TestNG For Desired Purpose

There are more annotations than the ones defined above, which are used for desired purpose only.

@DataProvider

This annotated method is used for supplying data to the test method in which the dataProvider attribute is defined. This annotated method helps in creating a data driven framework where multiple sets of input values can be given which returns a 2D array or object. @DataProvider annotation in TestNG comes with two attributes.

  • name- this attribute is used to provide name to the dataprovider. If not set it defaults to the name of the method provided.
  • parallel-this is one attribute that helps in running your tests in parallel with different variation of data. This attribute is one of the reasons to make TestNG more powerful to Junit. Its default value is false.

Below is the code snippet indicating the use of @DataProvider annotation with name and parallel attribute set to it.

@Factory

This annotation helps to run multiple test classes through a single test class. It basically defines and create tests dynamically.

The below code snippet indicates the use of @Factory annotation that helps calls the test method class.

Console Output:

Factory_Console

@Parameters

This annotation helps you pass parameters to your tests directly via the testNG.xml file. Usually this is preferred when you have limited data sets to try on your tests. In case of complicated and large data sets @dataProvider annotation is preferred or excel.

The below code snippet showcase the same:

The parameter values are defined in the TestNG.xml file as below:

@Listener

This annotation helps in logging and reporting. We have multiple listeners like:

  • IExecutionListener
  • IAnnotationTransformer
  • ISuiteListener
  • ITestListener

But to go in depth with these listeners and their uses would be a talk for another blog. I will be writing one soon, so stay tuned.

That Was All!

The key point to note while working with all these annotations and attributes is your system should have java 1.5 version or higher as these annotations are not supported for all lower versions of java and you may tend to receive error for them.

All the above mentioned annotations and attributes of TestNG helps to provide better structuring and readability to the code. It helps provide detailed reports that makes status reporting part even easier and useful. Use of these annotations in TestNG for Selenium completely depend on your business requirements. Hence, choosing the right ones for the right use is important.Try out these annotations in TestNG on LambdaTest Selenium Grid ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/support/docs/testng-with-selenium-running-java-automation-scripts-on-lambdatest-selenium-grid/) now!

How To Use Name Locator In Selenium Automation Scripts?

Written by  on October 15, 2019

Posted by Sadhvi Singh | April 4, 2019 Automation • Selenium Locators • Selenium Tutorial •

 

Locators in Selenium play an important role in the life of an automation engineer. Depending on how skilled you are in locating an element is directly proportional to the stability and efficiency of your automation testing with Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/automation-testing-with-selenium-cucumber-testng/). There are multiple ways of utilizing locators in Selenium to find an element on a page but deciding the right one, does the business. In this article, I will be referencing on how to use name locator in Selenium automation scripts ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/).

You can check out other articles around different CSS locator in Selenium that helps in locating elements through various ways:

  • Link Text & Partial Link Text In Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/using-link-text-and-partial-link-text-in-selenium/)
  • ID locator in Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/making-the-move-with-id-locator-in-selenium-webdriver/)
  • Class Name locator in Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/selenium-java-tutorial-class-name-locator-in-selenium/)
  • Tagname locator in Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/locating-elements-by-tagname-in-selenium/)
  • CSS Selector in Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-pro-testers-use-css-selectors-in-selenium-automation-scripts/)
  • XPath in Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/)

If you are an advanced or medium Selenium practitioner, then you can chuck on dedicated articles mentioned above. And go for our complete guide to help you illustrate the practical demonstration of CSS locator in Selenium.

Check Out My Complete Guide For Every CSS Locator In Selenium WebDriver With Examples ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/locators-in-selenium-webdriver-with-examples/)

Understanding The DOM(Document Object Model)

The Document Object Model(DOM) defines the structure of a webpage which constitutes of various tag elements and their corresponding attributes. For example, below shows the LambdaTest login page:Understanding The DOM

In the screenshot above, we are trying to locate the ‘email’ field. The DOM structure of the email field is displayed below:

< input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg" >

The attributes for the above DOM structure are:

  • Type
  • Name
  • Value
  • Placeholder
  • Required
  • Autofocus
  • Class

Understanding The Name Locator In Selenium

Name locator in Selenium is used to identify the elements of a webpage. This attribute can be mentioned as part of multiple tags like < input >, < button >, < select > etc. Unlike ID, this may or may not be unique to a page. A webpage may contain multiple tags with the same name attribute value. In such a case, if your intent is to select your desired element, the name locator in Selenium may not be the correct choice.

A key point to note is when you tend to select an element with a desired name attribute value, it will select the first element it encounters. You may come across another scenario, where you wish to select elements with the same name attribute value, in this case, the Name locator in Selenium may work using the findElements syntax. I will show both scenarios example in the code as we go ahead in the article.

In order to locate element via the Name locator in Selenium, we use the below command:

driver.findElement(By.name(“Element NAME”));

Let’s dig into the code snippet to understand the usage of the name locator.

Test Scenario 1 For Name Locator In Selenium

In the below code example, we are logging on to the LambdaTest platform. Here we will locate the ‘email’ and ‘password’ field using the name attribute.


Download Whitepaper
^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/#)

Test Scenario 2 For Name Locator In Selenium

In the below code example, post-logging in, under the automation section of the menu bar, we have a page where two web elements containing radio button have the same name attribute value as radio. We intend to switch between these two buttons. Below is the DOM structure for both those elements:Name Locator In Selenium

The code snippet below:

As you can see in the above two examples, we have used the name locator in different ways. One was via findElement and the other was via findElements command. In one, our target was to locate a single element while in the other we located multiple elements with the same name and attempted to switch to one of those elements. So as per defined need and business, we can use the Name locator. Name locators are easy to use and maintain, the trick lies in identifying their right usage.

You can also read my previous article where I have explained the demonstration of all the locators in Selenium WebDriver ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/locators-in-selenium-webdriver-with-examples/). I will also be writing down a series of dedicated articles to demonstrate thorough usage of each locator of Selenium in detail. Stay tuned! 🙂

How To Measure Page Load Times With Selenium?

Written by  on October 15, 2019

Posted by Himanshu Sheth | March 11, 2019 Automation • Cross Browser Testing • Selenium Tutorial •

 

There are a number of metrics that are considered during the development & release of any software product. One such metric is the ‘user-experience’ which is centred on the ease with which your customers can use your product. You may have developed a product that solves a problem at scale, but if your customers experience difficulties in using it, they may start looking out for other options. Website or web application’s which offers better web design, page load speed, usability (ease of use), memory requirements, and more. Today, I will show you how you can measure page load time with Selenium for automated cross browser testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/selenium-automation). Before doing that, we ought to understand the relevance of page load time for a website or a web app.

Why Focus On Measuring Page Load Time?

More than 40 percent of the website visitors may leave if the loading time is more than 3 seconds. This is why your software development, design, and product development team should focus on improving the performance of your web product. Don’t believe me? Below, is an image from a study conducted by Google ^(http://seoblackhatforum.com/goto/https://think.storage.googleapis.com/docs/mobile-page-speed-new-industry-benchmarks.pdf) related to page load speed with respect to bounce rate Page Load Time

Popular search engines measure the time it takes for the receipt of the first byte in order to determine the site speed/page speed. Hence, slow rendering pages can have a huge hit on the search engine rankings. Slow page speed also means that the search engine crawler can crawl less number of pages within the allocated crawl budget, which would eventually show up in the rankings.

A fact known to most of us is that if a website offers a more mobile-friendly user experience with faster page load time on mobile, Google would value those websites more and rank them on top in mobile search results. For this purpose, Google came up with a new mobile-friendly algorithm on April 21, 2015. Therefore, it is important to test a mobile website ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-to-test-a-mobile-website-using-lambdatest/) for user-friendliness.

It is not just Google but your customers would also won’t prefer your website or web app if the pages on the website don’t load with the lightning fast pace. Even one-second delay in the page load time can result in higher bounce rates and reduced number of page-views. It also has a negative impact on Click Through Rates (CTR) and conversions. In simple terms, measuring page-load time (also referred to as Page Speed) is the amount of time it takes for the content/page to appear on the screen. Lower the page-load time, better is the usability factor of the product, better is your conversion rate.

Metrics To Consider For Measuring Page Load Time

Different pages on the same website/web-app can have different load times, depending on the design being used for the development of the pages. Developers might have used either of the techniques to reduce the page load time.

a. File compression techniques for image optimization.
b. Browser caching.
c. Reduced number of redirects.
d. Reduced usage of non-blocking JavaScript.
e. Images of variable sizes depending on the device being used by the customer

That is from the developers’ end, but how would you go about testing the website by measuring the page load time of every page?

There could be hundreds of webpages on your website. Not to forget, you would have to perform browser compatibility testing to realize the page load time of every page on your website across hundreds of browsers and browser versions.

There is a possibility that your web page load time will differ if accessed from different browsers (including browser versions). It would also depend on the platform (desktop, tablet, mobile, etc.) and the geographical location from where your website/web-app is being accessed. You can’t go about testing every combination(web page + browser + browser version + operating system) manually. This is where Selenium grid ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/selenium-grid-setup-tutorial-for-cross-browser-testing/) comes to the rescue by empowering you to perform automated cross browser testing of your web pages. You can perform automation testing for measuring page load time with Selenium. Before we do that, it is important to have a look at the Navigation Timing.

Using Navigation Timing To Measure Page Load Speed

So, how do you measure page load speed? Navigation Timing is a popular JavaScript API which can be used for accurately measuring the performance on the web. Though there are other libraries available, Navigation Timing is more reliable & accurate.

The Navigation Timing API is available in most of the popular web browsers namely Firefox, Chrome, Internet Explorer, etc. Though there is the number of interfaces available for Navigation Timing, most of the details related to page load time can be accessed using the properties of the window.performance object. The window.performance returns more accurate results since the API makes use of HRT (High-Resolution Time) to calculate the necessary timings related to a web page.

The important properties of the window.performance object are

navigation – Details about how the user navigated to the web page.
timing – Information about the navigation & page load events.

Other interfaces that are available as a part of the Navigation Timing API are PerformanceNavigationTimingPerformanceTimingPerformanceNavigation. Performance data is pushed from the client side to server side with the help of an XHR(XMLHttpRequest). Using the Navigation Timing API, you can keep a tab on every request. Time is taken for loading the new page, the time is taken for unloading the previous page, time encountered for looking up the domain name, window. onload total time, response start time, etc. are some of the properties that can be accessed via the window.performance property. Below is an image representing the elements of PerformanceTiming interface.

PerformanceTiming

The timing property returns important performance information about the current page/document e.g. details about navigationStart, connectStart, connectEnd, domainLookupStart, domainLookupEnd, and more.

The figure below shows the order of performance of navigation.timing events. Each performance.timing attribute shows the time of the navigation event i.e. when the previous page was unloaded? when the latest page was requested? when the Document Object Model (DOM) began loading the page? and more. The timing is in milliseconds which is calculated since midnight of January 1, 1970 (UTC). You can have a look at the official W3C documentation on PerformanceTiming interface ^(http://seoblackhatforum.com/goto/https://www.w3.org/TR/navigation-timing/#sec-navigation-timing-interface) in order to get more information about each event.

order of performance of navigation.timing

Though you can access the window.performance through your source code, in order to see window.performance in action, you can access the option via the debug console of the browser (Firefox, Chrome, etc.). For example, open the LambdaTest ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/) website in Firefox/Chrome browser. In our case, we are using the Firefox browser. Now open the Inspect Element option to go to the debug console. Open the console and key in window.performance.timing in the console window. As seen from the output window, window.performance returns the performance object which also contains the timing property.cross browser testing

Further details about Navigation Timing API and corresponding browser compatibility can be found here ^(http://seoblackhatforum.com/goto/https://developer.mozilla.org/en-US/docs/Web/API/Navigation_timing_API).


Download Whitepaper
^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-to-measure-page-load-times-with-selenium/#)

Automation Testing To Measure Page Load Time With Selenium

You can make use of Selenium to measure the performance of a web page over a period of time. The results that are obtained from the Navigation Timing API can be further used to improve the page load time with Selenium testing and also benchmark those results against your competitors’ web performance. Though there would be visible differences between the home page & functionalities provided by two websites/web-apps (solving a similar problem), it is still important that the page load speed is equal (if not better) than your competition.

Along with tracking & benchmarking the web-page performance, Selenium can also be used to log the statistics of the page to come up with improvements on the client-side. As far as the server side is concerned, major performance improvement can be obtained by making use of the right kind of infrastructure.

Example 1 – Capturing Statistics Of A Web-Page Timings With Navigation Timing API

Let’s have a look at an example where Python is used along with the Selenium framework to capture important statistics about the web-page (e.g. lambdatest.com). It is an example that you can use to get started to measure page load time in Selenium.

To start with, an instance of Chrome web-driver is created and the web page under test is opened in the browser. We make use of Performance Timing API to obtain the amount of time spent in each event from the time the request was made from the browser. The execute_script option is used to synchronously execute JavaScript code in current window/frame.

For the demo, we have used the following metrics:

  • navigationStart – This attribute returns the time spent after the user agent completes unloading the previous page/document. If there was no document prior to loading the new page, navigationStart returns the same value as fetchStart.
  • responseStart – This attribute returns the time as soon as the user-agent receives the first byte from the server or from the local sources/application cache.
  • domComplete – This attribute returns the time just before the current document/page readiness is set to ‘complete’. document.readyState status as ‘complete’ indicates that the parsing of the page/document is complete & all the resources required for the page are downloaded. We will have a look an example of domComplete in subsequent section.

window.performance. timing.navigationStart, window.performance.timing.responseStart, and window.performance. timing.domComplete are the Timing Navigation options which are passed as arguments to execute_script() API. The measurement of time is in milliseconds (ms).

We have used Eclipse as the IDE for development & testing of the test code. In order to execute the code, press (CTRL + F9) from your Eclipse IDE. Below is the output once the above code is executed, the output shows the time spent in the front-end & back-end activities of the web-page.Navigation Timing API

Example 2 – Using WebDriverWait Interface For Element Search On A web-page

We have a look at another example where we load a web-page and search for the presence of an element (div-id) in the page. We search for the div-id for timeout duration of 5 seconds. Once the page is loaded, div-id (in our case: owl-example) is searched for 5 seconds, after which an exception is raised if the owl-example div-id is not present on the web-page. All the necessary modules are imported at the start of the implementation.

Like the previous example, we initialize the Chrome webdriver interface. The web-page under test is the LambdaTest homepage – lambdatest.com. The timeout duration is set to 5 seconds. There are two test-cases, details of which are mentioned below

  • Test case 1 – The web-page being loaded is checked for the presence of div-id ‘owl-example-1’. Using the WebDriverWait interface, search is performed for 5 seconds. If‘owl-example-1’ is not present on the page, an exception is raised.
  • Test case 2 – The test condition is same as Test case 1, except that the div-id under search is ‘owl-example’

Below is the output of the above example

As expected, the first test case fails as the div-id under search is not present on the web-page under test. The search times out after 5 seconds. On the other hand, the second test case passed since the div-id ‘owl-example’ is present on the web-page. For confirmation, please visit the HTML source of LambdaTest ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/). (which is the page being tested)lambdatest

There would be scenarios where you need to perform an operation when a webpage is loaded i.e. document.readyState status as ‘complete’. The operation could either be clicking on a hyper-link or clicking on a button or any other operation. It could be a tricky situation as there might be scenarios where few sections of webpage are loaded asynchronously using AJAX (Asynchronous JavaScript and XML) on the client side. We focus on webpage operations where a new webpage is loaded. In this section of the Selenium testing tutorial, we have a look at the mechanism that Selenium uses to check whether the complete webpage is loaded.

Example 3 – Detection Of Page Load Completion With pytest & Selenium

Selenium has an in-built condition called staleness_of and its own wait_for implementation. staleness_of is the element of the selenium class selenium.webdriver.support.expected_conditions. staleness_of will wait until an element is not attached to the DOM. Below is the implementation of the class

element is the actual element on the webpage to wait for. It returns False if the element is attached to the DOM, else it returns True. We will make use of the pytest framework which we have already covered in earlier blogs. In the below example, @contextmanager is used which aids in allocating & releasing necessary resources when required.

In wait_for_page_load(), search for the div-id owl-example is performed. WebDriverWait is used in combination with ExpectedCondition to achieve Explicit Wait condition. The driver and timeout are passed as arguments. We wait until the element gets stale. In case the element being searched i.e. owl-exampl’ is not present, a TimeoutException is thrown. This is how we check for the staleness of the element.

In test_click_operation(), the webpage lambdatest.com is loaded after which a wait for the maximum duration of 10 seconds is performed. It uses wait_for_page_load() to check whether the element owl-example on the webpage is not stale. Once the webpage is loaded within the timeout, we check if there is an element which has link text FREE SIGN UP which is a button that is hyperlinked to the registration page. Finally, we check the status of web page/document using the Document.readyState property. When the value of this particular property changes, a readystatechange event is fired on the document object.

The three possible states are loading, interactive, and complete. Document.readyState achieves the state as complete if the webpage/document & its dependent resources have finished loading. In our example, if the page is loaded completely, a button click is performed which eventually takes us to the LambdaTest registration pagecross browser testing

For execution, py.test command is used with verbose enabled and –capture set to noimage5 (4)

Depending on the target market and the browser that is responsible for bringing you the most traffic, you should create a cross browser testing matrix ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/creating-browser-compatibility-matrix-for-testing-workflow/) to perform cross browser testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/performing-cross-browser-testing-with-lambdatest/) in an organized manner. Cross browser testing of your product would help you with the findings of valuable test cases which can further be used for improving the page-load times. Rather than creating a local infrastructure to do these tests, you can make use of LambdaTest which offers scalable cross browser testing tool on the Cloud.

Conclusion

Measuring page load time is very important for web-based products since it has a direct impact on the user-experience. Along with optimizing the overall design of your product, it is important to design web pages in a manner that the page load times are kept to the minimum. This would improve the conversions expected out of the product.

Navigation Timing APIs are instrumental in keeping a measure of page load times. You can use the output generated by these APIs for optimization purpose. window. performance returns the performance object which gives important information about the current document/page. It can be used along with the Selenium framework. Measuring page load time with Selenium is critical as a web page can vary across different types & versions of browsers, hence it is important to focus on optimizing the page speed of your product on browsers & platforms for superior customer experience.

Cross Browser Automation Testing Using Watir

Written by  on October 15, 2019

Posted by Muthuraja | January 10, 2019 Automation • Cross Browser Testing • Selenium Tutorial •

 

We are living in an era where software development demands for automation. Software development methodologies such as RAD(Rapid Application Development), Agile and so on requires you to incorporate automation testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/automated-cross-browser-testing/) as a part of your release cycle. There exist numerous test automation frameworks used for automation testing. Today, I will be picking up Watir an open source, selenium-based web driver used for browser automation. Cross browser automation testing using Watir would help you to ensure a good rendering user interface of your web app. If you are a beginner to automation testing and are unaware of basics then don’t worry as I will also be talking about browser automation, cross browser automation, parallel testing and what makes Watir special than other several tools and libraries. Without further ado, here we go!

What Is Browser Automation?

Browsers have become indispensable to our daily basis productivity. We explore, learn, shop, invest and do a lot more with browsers. There are times when we feel the need for browsers to automate our repetitive tasks. Imagine how cool that would be? Is it possible? Yes, any task you perform in browsers is automatable.

Let us take some considerations based on basic practical scenarios. You can use browser automation if:

  • you need to fill a lot of forms for data entry.
  • you need to generate a report from web portal on daily basis and send email to the team.
  • you navigate to a specific website and upvote on daily basis for a particular candidate on a reality TV show.

In the software industry, testing a web app can demand for a huge investment of time, resources and money. Testing a web app to check how well it renders could be very strenuous if relied upon manual testing alone. The manual testing ensures your application is functioning as expected, based on client requirements and expectations. Browser automation comes to rescue so we could avoid repeated manual testing, regression test cases, and obtaining early feedbacks along with reduced effort.

What Is Cross Browser Automation Testing?

Now, there are thousands of browsers available on the internet and your website may render in a unique manner on every browser or browser version. It could be very complex to test your website across every single one of them. Cross browser automation testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/automated-cross-browser-testing/) is to verify your application renders seamlessly across different browsers and its combination version. This is primarily performed to check cross browser compatibility of your shippable application’s functionality on combinations of the various OS running separate browsers like Google Chrome, Mozilla Firefox, Edge, IE, Opera, Safari, Yandex and many more.

To run all browsers and its combination of version is time-consuming. For eg., your client asks to perform a regular basis cross browser compatibility check across 5 versions of latest and 5 versions of legacy Chrome, Safari, Firefox and Edge browser that would bring a total number of browser combinations to be of 40 completely different distinctive browsers and its version for one test case. Consider how overwhelmingly time-consuming and tedious it could be to perform a cross browser compatibility ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/feature) check over all those browser combinations and provide sign off, that needed positively proper planning, huge investments in QA resource, handling challenges, time and cost. To deal with this issue, Cross Browser automation testing for parallel test sessions is the solution.

What Is Cross Browser Parallel Test Automation?

Cross Browser Parallel testing is performed to run a single test across multiple browser combinations, simultaneously. This is a very practical and powerful consumption scenario of automation testing. Cross browser parallel test automation allows you to scale back execution time while not compromising with coverage of your check and leading to faster rotation of your test results.

What Is Watir?

Watir is an open source Ruby Libraries which helps to achieve cross browser automation testing. Watir supports Ruby which is object oriented language and typically it’s simpler and faster than other languages. The good thing about Watir is, it supports any web application irrespective of the technology used to develop that application.

Why Watir?

  • It’s a free open source tool belonging to a family of Ruby.
  • It supports headless browser execution.
  • It supports page object design pattern.
  • It supports Cucumber Integration.
  • Tests can be maintained simple and flexible.
  • It supports your web app no matter what technology used to develop your app.
  • It supports multiple browsers on different platforms.
  • It is lightweight and easy to use.
  • It supports execution in Cloud through cloud based cross browser automation testing tools like LambdaTest.

In this article, we will take a look on how to setup test automation environment for Watir with RubyMine IDE and then go ahead with the sample script. I would also show how to run the script and see the failure in RubyMine. Let’s dive right in.

Getting Started With Cross Browser Automation Testing Using Watir

Here are the prerequisite required for performing cross browser automation testing using Watir on Windows:

  • RubyInstaller
  • Watir
  • RubyMine IDE

The RubyInstaller is must because Watir supports Ruby code, so this installation is very important before we proceed with Automation.

Setting Up Automation Environment

This section has detailed steps explaining how to setup automation environment for performing cross browser automation testing using Watir through Windows.

Step1: Installation of Ruby:

Navigate to Official Ruby Installer page here ^(http://seoblackhatforum.com/goto/https://rubyinstaller.org/downloads/).

Click on Latest version With DEVKIT to download. Select your OS architecture type, for e.g., I select (X64) 64 bit operating system.

lambdatest

After download, right click on installer and run as administrator in Windows machine.

lambdatest

The setup window popups to accept license agreement, you need to accept this and move next.

lambdatest

I would recommend selecting all checkboxes and clicking install button to proceed further.

lambdatest

The below progress bar indicates the install is in-progress, which takes few minutes time to install ruby.

lambdatest

After installation is complete, the following window will popup to install updated component. This is optional either you can leave it by closing the window or hit “Enter” button to update all components.

lambdatest

Open command prompt in windows and enter the following command:

  • ruby -version

The installation version of Ruby version is displayed in the command prompt, which indicates Ruby installer is success.

lambdatest

To verify once again the installation is success and Ruby programs works fine. Enter the following commands:

  • irb

irb or IRB stands for Interactive Ruby Shell which is a REPL(read-eval print loop) for programming in the object-oriented scripting language Ruby.

I will write a simple code that will put “lambdatest.com” to print it.

lambdatest

Step2: Installation of Watir:

Installation of Watir is very simple. As I mentioned in the above section to verify Watir, you must first installed Ruby. To install, supply the below gem command:

  • gem install water

gem indicates RubyGems that is a package manager for the Ruby programming which provides a standard format of disturbing Ruby libraries.

lambdatest

Step 3: Installation of RubyMine IDE:

RubyMine is an IDE(Integrated Development Environment) that helps you to write, debug and testing code for your application. Ruby also support multiple OS like Windows, MacOS and Linux etc.

Note: RubyMine comes up with Free 30- day Trial license.

To download the RubyMine, click here ^(http://seoblackhatforum.com/goto/https://www.jetbrains.com/ruby/)

Click on the following button, which will download in your machine.

lambdatest

Right click on installer and run as Administrator, which pop ups the following windows.

Click next and continue till the installation is complete and launch RubyMine

lambdatest

Keep default with existing plugins and click Next until you see the below window.

Click “Create New Project”

lambdatest

Enter project name and select “Ruby SDK” which will be available if you install Ruby Installer.

lambdatest

Right click on created project and create a new directory called “testsuites”

lambdatest

Create a subdirectory called “resources” under testsuites and drop “chromedriver.exe”

To run browser based automation, chromedriver is required which is a separate executable that WebDriver uses to control chrome. Similarly we have different driver for each browser for eg., geckodriver for Mozila firefox, edge driver for Microsoft Edge browsers etc.,

To download chromedriver, please visit here ^(http://seoblackhatforum.com/goto/http://chromedriver.chromium.org/downloads).

lambdatest

Writing Code for Sample Test

To write code in RubyMine is very simple.

Right click on testsuites and select “TestUnit Test Template”

lambdatest

Enter file name “my_script.rb” in filename and click ok button

lambdatest

On the RubyMine window that you are observing now, write the following code

CODE (Self Explanatory):

Right click on the script and select “Run” in IDE to see test results and what is the output

lambdatest

The above test script I intentionally fail to verify results in terms of comparing actual vs. expected page title.

The below snapshot explain the test gets failed and the difference is highlighted in colour.

lambdatest

Using Local WebDriver To Perform Cross Browser Automation Testing Using Watir

Code Explanation:

In TestUnit template,the IDE automatically create def setup and teardown. These methods actually run before and after test.

Setup: This method is precondition to run the test, you may setup something like initialize browser or any test data setup and

Teardown: This method is after your test run, you may close.quit the browser or delete the data in database etc.,

The above code is initializing the chrome driver and create new instance and assign object reference to variable browser.

The next line of code is typical Watir script which actually steps of:

Note: The method name start with “test_” is mandatory which Unit Test Runner actually realize this is test method.

  • Navigate to browser
  • Maximize the Window
  • Click on “Start Testing” button in lambdatest.com
  • Prints Current Page Title
  • Finally verifying whether expected vs actual title

Using Remote WebDriver At LambdaTest To Perform Cross Browser Automation Testing Using Watir

The drawback of executing cross browser automation testing using Watir for Local WebDriver is that you only get to test your script over the browsers installed in your local machine. However, it isn’t feasible to install thousands of browsers on your system. This is why, cloud is a more preferred platform to perform cross browser automation testing. Cloud provider offers to run your test scripts with support of Selenium grid. The great factor is, you don’t have to be compelled to maintain or any setup needed, all you wish to try and do is slightly tweak your code to support selenium grid. I’m going to explain how to tweak your same code to run test in Lambdatest. LambdaTest provides an extensive Selenium grid offering more than 2000 browsers to test from. You can even perform cross browser automation testing of your locally hosted web apps using their SSH tunnel. Let me show how you can leverage LambdaTest for browser automation using Watir.


Download Whitepaper
^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/cross-browser-automation-testing-using-watir/#)

Run Single Instance In LambdaTest Using Watir

LambdaTest provide cross browser parallel test in terms of running your cross browser automation testing scripts parallely to reduce great execution efforts. We will take a look in detail below regarding how to run simple test and how to run same set of test with different browsers in LambdaTest.

I’m using same script which I connected in my previous section in above, except the subsequent changes in setup techniques as mentioned within the below snapshot.

LambdaTest supports Selenium grid, so to run your test in LambdaTest you need to initialize remote webdriver and point where your Selenium hub or server is running.

Template of Selenium Grid URL to run in LambdaTest is:

https://{username}:{accessToken}@hub.lamdatest.com/wd/hub

User Name:You can find it in LambdaTest dashboard page.

Access Token: You can find it on your profile page. In case no token, feel free to click on generate Access Token button.

Grid URL: hub.lamdatest.com where your selenium grid is running.

The other important feature is the Capabilities Generator ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/capabilities-generator/): To run your test suites in LambdaTest Selenium grid you must set the desired capabilities for example, which browser, which operating system and so on basic configurations that you want to test on.

You can configure which browser, platform etc., and finally it generates a code based upon your selections. Below is the result for Ruby language.

lambdatest

Capabilities Generator also supports Java, JavaScript, C#, Python, and PHP.

That’s all you need to run your tests successfully at LambdaTest for performing cross browser automation testing using Watir.

Run Cross Browser Parallel Testing With Watir Using LambdaTest

So far you have got seen a way to run single instance in LambdaTest, this section is detail about regarding a way to run same set of test with different totally browsers in LambdaTest. This section would assist you to grasp a way to generic code run different browser in Watir.

The below code would facilitate to realize the parallel execution:

lambdatest

lambdatest

The logic is simple, used Thread concepts to run the same set of test scripts in parallel. The thread creates dynamic based on how many browsers and its version are added in BROWSERS variable.

I passed argument username, password in setup method for demo purpose but for more customization please embody multiple parameters and replace with capabilities.

Well, now you have your test results look in LambdaTest Cloud platform. Please take a look on below snapshot.

lambdatest

Kudos! We made it, with this you have successfully run your first script for cross browser automation testing using Watir at LamdaTest cloud platform.

LambdaTest offers great in-depth detail for every test. If you look at the screenshot below, you will be able to notice the environment details, status, start time, end time and many more information that is automatically represented. You would also find a video log of your test session and analytics related tab alongside, timeline.

lambdatest

That was all you need to know for getting started with cross browser automation using Watir at LambdaTest to perform UI test for your website or web-app. I would be interested to know your thoughts and feedbacks on this tutorial in the comment section. I would appreciate if you have got any questions for me. Happy Testing!

Automated Cross Browser Testing With Protractor & Selenium

Written by  on October 15, 2019

Posted by Muthuraja | January 22, 2019
Automation • Cross Browser Testing • Selenium Tutorial •

 

We are living in an era where numerous test automation tools or libraries are available in the market. The abundance brings along confusion and choosing the right framework turns out to be very challenging. Identifying the right tool at the earliest is crucial to for greater ROI. When come to Angular JS based application we don’t have to worry too much because we have Protractor in place. Google strongly recommends Protractor for end-to-end automation for AngularJS because both are maintained and owned by Google, and they build JavaScript test automation framework to handle AngularJS component in a better manner. In this article, we will be looking into details about what do we mean by JavaScript test automation Framework? What is Protractor? Why Protractor Is a good fit for test automation? How it helps to achieve cross browser test automation with different browsers?

What Do We Mean By JavaScript Test Automation Frameworks?

There has been major growth in JavaScript Framework usage over the past few years, the reason is, nowadays most of the apps are developed using technology like Angular JS or React JS. In fact, initially, AngularJS was the first choice for web developers ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/angularjs-a-developers-first-choice/), however, by the end of 2018, we could see major dominance over the web for ReactJS. ReactJS may even continue to dominate 2019 as well ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/will-reactjs-continue-to-dominate-in-2019/). To deal with these in test automation, we should adopt right programming language and test automation framework.

JavaScript test automation frameworks are end-to-end framework is specialized to run automation scripts for web application. However, choosing the right could be very challenging based on the following criteria selection.

  • End to End Testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/all-you-need-to-know-about-end-to-end-testing/)
  • Assertion library
  • BDD/TDD Integration
  • Integration test
  • Regular updates from the community
  • Cross Browser Layout Testing
  • Visual Regression Testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/smart-visual-ui-testing)

Why JavaScript Framework Is Necessary When Compare With Other Languages?

JavaScript is no news to web developers. Most of the developers in an organization write JavaScript code for developing web applications, unit test etc. Here are the following reasons for considering JavaScript framework pivotal.

  • Easy to follow Shift left testing for better product quality ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-shift-left-testing-can-help-your-product-quality/).
  • Provides developers the flexibility of generating end to end tests, along with unit testing for faster and reliable shipping.
  • More collaboration between Dev and QA in same language for test automation.
  • Helps to implement Dev QA test and pair programming in test automation side.
  • More flexible to work all in same languages and better productivity.
  • Right tools and right approach in same languages gets greater benefits.

What Is Protractor?

Protractor is a JavaScript framework, end-to-end test automation framework for Angular and AngularJS application. It test against your application in real browser, like a real user would.

Protractor is built on top of WebDriver JS (Selenium) and apart from default locator, it comes up with Angular-specific locator strategies. It’s not intended to test only AngularJS application but also helps to write automation test for Non-Angular JS application.

What Makes Protractor A Good Fit For Your Automation Test Scripts?

Protractor is an open-source, End to End test automation framework, that is brought up by Google which makes it a reliable, renowned and up-to-date framework. Protractor is a Node.js program that supports testing framework like Jasmine, Mocha and Cucumber.

Key Features of Protractor:

  • Protractor is developed as a wrapper build around Selenium WebDriver JS and Selenium Server. All key feature of Selenium WebDriver JS and additional implementation helps to do better automation supports and less maintenance when compare with Selenium.
  • Protractor offers new locator strategies on top of native locator which actually helps to locate element in DOM easily.
    • by.model, by.binding, by.repeater etc.,
  • Protractor supports page object design pattern – helps on writing clean and readable code. Writing clean code is imperative for Automation testers. Here are 8 actionable insights for writing better automation code ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/8-actionable-insights-to-write-better-automation-code/).
  • Protractor make use of Selenium Grid to initialize, control and run browser instance.
  • Can easily integrate with jasmine, mocha and cucumber framework to write your test.

Advantage of Protractor when combined with Selenium:

  • Excellent speed compare with other tools or libraries.
  • Support cross browser parallel testing through multiple browsers.
  • No need to work on synchronization strategy for eg., waits like implicit, explicits and sleep etc., Protractor has in build capabilities and intelligence to connect with Angular JS framework and knows when Angular has finished rendering the DOM for eg., page loading wait, synchronization,
  • Angular-specific locator support.

Getting Started With Cross Browser Automation Testing With Protractor and Selenium

Here are the prerequisite required for performing cross browser automation testing with Protractor and Selenium on Windows:

  • Node.js
  • Install protractor using npm
  • Update webdriver manager and start up a server
  • Visual Studio Code

Setting Up Automation Environment For Testing With Protractor And Selenium In Windows

This section has detailed steps explaining how to setup environment for performing end to end cross browser automation testing with Protractor and Selenium through Windows.

Step 1: Installation of Node.js

You need to install npm for Node.js.

What is npm?

npm is a free to use, package manager for javascript development which helps you to download all public software packages

What is package manager

A package in node.js contains group of files that you need for a module. Module is nothing it’s javascript libraries that can be included in your project.

To install Node.js, click here ^(http://seoblackhatforum.com/goto/https://nodejs.org/en/).
image-lambdatest

Once you are done downloading the installer, run it as administrator.

Click on ‘Next’ button in Node.js setup window to proceed further.

image-lambdatest

Click on checkbox for accepting license agreement and click on Next button.

image-lambdatest

Keep default windows location or feel free to change the location and click next.

image-lambdatest

Thereafter, click on Next and Install button. Wait for a couple minutes for the installation to complete.

You would have seen the following screen after installation completed.

image-lambdatest

To verify npm gets installed in your machine. Please supply the following command:

npm –version
image-lambdatest

Step 2: Installation of Protractor using npm

To download a package, open a command prompt and supply the following command

  • npm install –g protractor

install command is for download the package

-g stands for global which means any command to operate in global mode.

The below snapshot indicates that protractor is installed successfully.

image-lambdatest

To verify installed protractor version, supply the following command

  • protractor –version

image-lambdatest

Step 3: Update WebDriver Manager and Start Up A Server

To update webdriver manager, supply the following commands

  • webdriver-manager update

Once supply the above commands, it would download all necessary dependencies.

image-lambdatest

WebDriver manager is a helper tool to download necessary binaries. It will help to start and stop selenium server.

To start up with selenium server,

webdriver-manager start
image-lambdatest

To verify selenium server is running in browser, please use the following url

http://localhost:4444/wd/hub

Protractor test will send request to this server and control a local browser where you can see information about the status of the server. So, you could later go ahead and run your JS automation script with Protractor and Selenium.

Step 4: Installation of Visual Studio Code

What is Visual Studio Code?

Developed by Microsoft, Visual Studio code is a source code editor for Windows, Mac and Linux operating systems. It is a lot similar to other programming editor like Eclipse or Intellij. This tool was developed by Microsoft for Windows, Linux and Mac OS. It supports number of programming languages, to know more about Visual Studio code please visit here ^(http://seoblackhatforum.com/goto/https://en.wikipedia.org/wiki/Visual_Studio_Code).

Double click on Visual Studio installer and click “Next” button.

image-lambdatest

Click on “I accept the agreement” in Setup – Visual Studio Code window

image-lambdatest

Keep default installation location (or) feel free to change different path in your computer and click “Next’ button.

image-lambdatest

Continue to click “Next” and check Desktop Shortcut if you want, finish the installation.

Once installation is complete, the following window would appear.

image-lambdatest

Click “Finish” button, finally Visual Studio Code Editors open up.

image-lambdatest

Click on the language which you want to install, I would suggest you to install JavaScript and TypeScript for executing automation test cases using Protractor and Selenium. Once clicked, it prompts you to select “ok” button, it would take few seconds to install the selected languages.

Writing Sample Code for Automation Testing With Protractor and Selenium

Now, we will have a look at a sample Javascript code for automation testing with Protractor and Selenium, and execute the test in local instance meaning execute the test that run on your browser installed in your machine.

To run protractor test for end to end automation, you must need two files:

  • Specs
  • Config

Specs: Specs is actual test suites that have more than on test cases designed in Javascript using Jasmine framework.

^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/automated-cross-browser-testing-with-protractor-selenium/#)


What is Jasmine Framework?

Jasmine is a BDD(Behaviour Driven Deployment) testing framework for Javascript. This framework helps you to write clean and better readability of code. To know more about Jasmine Framework, please visit here ^(http://seoblackhatforum.com/goto/https://jasmine.github.io/).

Executing Single Instance For Automation Testing With Protractor and Selenium WebDriver(Local)

Here is the simple example config file and how to write basic code in Protractor using Jasmine Framework.

Config.js:

In the above snapshot shows the syntax of how to define config file to execute browser automation with Protractor and Selenium.

  • seleniumAddress: Protractor make use to Selenium Grid to run your test in specified browser.
  • specs: specs indicates test suite that you can mention in one or more spec files to run multiple tests.
  • Capabilities: This is the place where you can capability setting for your test configuration for eg., browser version, platform etc., in case no capabilities is provided then by default, it takes chrome browser in your machine.

We will see clear explanation of what is the above code indicating.

describe –> describe is the keyword from Jasmine to write syntax for test suites. This is definition of collection of test cases in your script file.

it –> is the keyword for test cases to meet specific condition. There can be more than one “it” added under describe in Jasmine framework.

In the above code, we launch the browser, navigate to sign up page, see the title gets matched and pass the results.

To run your test:

In Visual Studio Code: Go to View Menu-> Click “Terminal” and supply the following command

image-lambdatest

Syntax: To run your test suites protractor followed by config file name.

Note: Before run your test, please make to run selenium Grid by supply the following commands:

  • webdriver-manager start

image-lambdatest

After executing the above test, observed the following failures which is expected

image-lambdatest

The above screenshot indicates the website https://lambdatest.com ^(http://seoblackhatforum.com/goto/https://lambdatest.com/) is not developed using AngularJS component. This is best real time examples to realize how Protractor works while launching browser. As mentioned in “Advantages” topic in above, the Protractor basically tries to connect with Angular JS framework while launching application. The moment Protractor identified and realized it’s not an AngularJS component, it recommended us to turn off waiting for Angular.

To Address this problem: we have a method called “browser.waitForAngularEnabled(false);

Now, the method is added in the script and as you can see in the below screenshot, Protractor now supports with non-angular js application.

After executing the test, the test is passed successfully.

image-lambdatest

Executing Parallel Execution For Automation Testing With Protractor and Selenium WebDriver(Local)

In this example I’m trying to use same script like above except small tweak in conf.js page. So I copied existing config and update configuration details to run same test in multiple browsers to perform cross browser parallel testing.

In the above examples, I modified “multiCapabilities” and included firefox browser. This is the best advantage when we compare with other libraries/packages, no maintenance is needed in case of automation testing with Protractor and Selenium. Protractor supports in-build code to run cross browser automation.

image-lambdatest

This time we run both same tests in chrome and firefox which executed and pass the results. So far we have seen how to run the test in protractor for Non-Angular JS application. The below section will show how to run AngularJS application in parallel and cloud execution (using Lambdatest).

Executing Parallel Execution for AngularJS Application

To run parallel execution for AngularJS application, the configuration remains but I implement use async and await for right way to queue up events.

Make sure to disable promise manage in your config file.

Executing Cross Browser Parallel testing, Protractor & LambdaTest (Cloud)

There are always challenges and limitation to run your test with multiple version of browsers in local machines. You can’t think of installing thousands of available browser in your local machine to perform extensive, automated cross browser testing. To achieve more browser usage, remote execution in cloud is the only solution. LambdaTest offers 2000+ browsers to run your automation test scripts on their Selenium grid over a variety of devices and different browser version.

In this example, I’m going to explain how to execute cross browser parallel testing with Protractor and Selenium on LambdaTest cloud provider.

Configuration Details to run in LambdaTest:

In the above screenshot:

seleniumAddress: ‘https://:@hub.lambdatest.com/wd/hub’

User_Account: It’s your login account of LambdaTest

API_KEY: it’s access key which can be found in Profile page of your login. In case no API key, feel free to generate.

multicapabilities indicates to run your test on multiple browsers in parallel.

The other configurations mostly platform, browser specific and LambdaTest cloud provider specific to enable or disable the options.

I’m going to execute the following code on LambdaTest with Chrome and Safari in parallel.

image-lambdatest

The below results indicates the test is success and passed on lambdatest cloud platform.

image-lambdatest

Let’s take a look on example in cloud.

image-lambdatest

Detail snapshot of Test Cases in LambdaTest page for Safari Browser:

image-lambdatest

With this, we have seen how we can run simple scripts for Javascript automation testing with Protractor and Selenium WebDriver for both local and cloud execution of parallel testing.

Selenium Waits: Implicit, Explicit, Fluent And Sleep

Written by  on October 15, 2019

Posted by Sadhvi Singh | August 14, 2019
Selenium Tutorial •

 

Selenium waits for page load play an important part in your Selenium scripts. They help to make them less flaky and more reliable. Selenium provides multiple waits to provide adequate wait or pause in your script execution based on certain conditions. Thereby ensuring you don’t end up getting failed scripts as you perform automation testing with Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/). In this tutorial, we will be explaining the types of Selenium waits and sleep, there real-time examples and a comparison study on them. Let us start by answering a pivotal question “Why should we use Selenium waits?”

Why Do We Need Selenium Waits?

Majority of modern application’s front-end is built on JavaScript or Ajax, using frameworks such as React, Angular, or any other which takes a certain time for the web elements to load on the page, whenever that page is loaded or refreshed. Hence, in case you tend to locate an element in your script which is yet to load on the page, selenium will throw you ‘ElementNotVisibleException’ message.

Below code snippet will help you showcase the same problem as you execute automation testing with Selenium. In this code snippet, I am using an example of easemytrip.com, where post user selects the ‘From’ and ‘To’ destination with a date of journey, the web application takes a certain time to load the required flight details. In this case, without applying wait, the user tends to book the first flight from the list. Now, since the page hasn’t loaded yet, the script failed to find the ‘book now’ button. Resulting in throwing a ‘NoSuchElementException’. Code snippet and console output below:

Console Output:

I have made use of XPath for locating web elements using the scripts for automation testing with Selenium.

Read More: Complete Guide For Using XPath In Selenium With Examples ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/)

Selenium wait for a page to load helps in resolving this issue. There are different types of Selenium waits like Implicit wait and Explicit wait, that ensures the elements are loaded into the page before they are discovered by the Selenium script ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/) for further actions.

Types of Selenium Waits For Page Load

When performing automation testing with Selenium, we use the following types of waits as we generate our Selenium script:

  • Thread.Sleep() method
  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

Let us understand each one of these in-depth.

Thread.Sleep() For Automation Testing with Selenium

Sleep is a static method that belongs to the thread class. This method can be called using the reference of the class name i.e Thread. If you use Thread.sleep while performing automation testing with Selenium, then this method will stop the execution of the script for the specified duration of time, irrespective of whether the element is found or not on the web page. It accepts the time duration in milliseconds. The syntax for the same is:

Thread.sleep(3000);

The sleep function throws InterruptedException so it should be handled using a try-catch block as below:

Why Using Thread.Sleep() Is Not A Good Idea?

I will now be highlighting some disadvantages of using the thread.sleep().

  • Selenium Webdriver waits for the specified time, irrespective of the element is found or not. In case the element is found much before the specified duration, the script will still wait for the time duration to elapse, thereby increasing the execution time of the script.
  • If the element to be present does not appear after a static time and keep changing, then you will never know an estimated time needed for the sleep function. In case it takes more time than the defined time, the script shall throw an error. Which is why, if you are dealing with dynamic elements using Selenium waits then it is wise to not use Thread.sleep().
  • Thread.sleep is intended only for the element it is written prior to. In case you have two to fours elements which need to wait for a certain duration to load, you need to specify Thread.sleep as many times in that case. And if you do that! Well, you will find your script filled with Thread.sleep() syntax all over the place.

Owing to the above disadvantages, using Thread.Sleep() in your script creation is considered as a bad practice.

The below code snippet highlights the usage of Thread.Sleep() for automation testing with Selenium. In this example, we are using the same example of easemytrip above, where we will stop the thread execution, once the user clicks on search. The code works smoothly in this case, without throwing any error.

Now, what if I have another page of the same application that takes a certain time to load? In this case, I would not prefer to use, thread.sleep() multiple times in my script.

You may be thinking that if not Thread.sleep(), then which Selenium wait for page load will suffice the testing requirement?

This is where Implicit wait comes to rescue in such cases. Let us checkout Implicit Selenium wait in detail.

Implicit Wait For Automation Testing with Selenium

Selenium has overcome the problems provided by Thread.sleep() and have come up with two Selenium waits for page load. One of which is Implicit wait which allows you to halt the WebDriver for a particular period of time until the WebDriver locates a desired element on the web page.

The key point to note here is, unlike Thread.sleep(), it does not wait for the complete duration of time. In case it finds the element before the duration specified, it moves on to the next line of code execution, thereby reducing the time of script execution. This is why Implicit wait is also referred to as dynamic wait. If it does not find the element in the specified duration, it throws ElementNotVisibleException.

Another interesting thing to note about Implicit wait is that it is applied globally, which makes it a better option than Thread.sleep(). Meaning you only need to write it one time and it gets applicable for all of the web elements specified on a script throughout the WebDriver instance. Convenient isn’t it? The syntax to achieve the same is:

The default time for Implicit wait is zero and it keeps polling for the required element after every 500 milliseconds. Let’s see the code snippet below, showcasing the use of Implicit wait. In this example, I am using the same easemytrip example. In this case, we are going a step ahead and continuing the booking process, where the page takes more time to load. Here the page load issue exists for two pages, which we are dealing using a single line of code using implicit wait rather than using Thread.sleep() multiple times.

Now, here we are aware of the fact, that pages shall we loaded in a certain duration, but what if we do not know the element to be visible/clickable at loading time. As in the time of its appearance is dynamic and keeps on changing from time to time. In this case, Explicit wait will help you overcome this problem. Let’s investigate its detail.

Perform Automated Cross Browser Testing With Selenium Grid of 2000+ Real Browsers ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/landing-text-cta)

Explicit Wait For Automation Testing with Selenium

The Explicit wait is another one of the dynamic Selenium waits. Explicit wait help to stop the execution of the script based on a certain condition for a specified amount of time. Once the time goes overboard, you will get the ElementNotVisibleException. In a scenario where you do not know the amount of time to wait for, this explicit wait comes in handy. Using conditions like elementToBeClickable() or textToBePresentInElement(), one can wait for the specified duration. One can use these predefined methods using the combination of classes WebDriverWait and ExpectedConditions. In order to use this case, import the below packages in your class:

Post this one needs to create a reference variable for the WebDriverWait class and instantiate it using the WebDriver instance and providing the amount of Selenium wait for page load, one may need. The unit of time is in seconds. One can define it as below:

In order to use the predefined methods of the ExpectedCondition Class, we will use the wait reference variable as below:

Types of Expected Conditions:

Below are the few types of expected conditions commonly used as you perform automation testing with Selenium.

  • visibilityOfElementLocated()- Verifies if the given element is present or not
  • alertIsPresent()- Verifies if the alert is present or not.
  • elementToBeClickable()- Verifies if the given element is present/clickable on the screen
  • textToBePresentInElement()- Verifies the given element have the required text or not
  • titlels()- Verify the condition wait for a page that has a given title

There are many more expected conditions available, which you can refer through the Selenium official GitHub page ^(http://seoblackhatforum.com/goto/https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ExpectedConditions.html). Like Implicit wait, the explicit wait also keeps polling after every 500 milliseconds.

Below is the code snippet highlighting the usage of an Explicit Selenium wait. In this example, we are using the ‘rentomojo’ application, where a modal appears at a dynamic time on the homepage. Using explicit wait, based on the element visibility, we will wait for the element and close the pop-up. Referenced code:

Note- When implicit wait and explicit wait are given in conjunction, then they work on cumulative time, rather than on a single wait condition. For example, if the Implicit wait is given for 30 seconds and the Explicit wait is given for 10 seconds, then the explicit element it is looking for will wait for 40 seconds.

The Difference Between Selenium Waits: Explicit Vs Implicit

Now, since you are aware of the usage of implicit and explicit waits, let us investigate the difference between these 2 Selenium waits:

IMPLICIT WAIT
EXPLICIT WAIT
It is by default applied to all the elements in the script. It is applicable to only a certain element which is specific to a certain condition.
We cannot wait based on a specified condition like element selectable/clickable unlike explicit. In explicit, we can specify the wait based on a specific condition.
It is usually used when you are sure the element may be visible in a certain time It is usually used, when you are not aware of the time of the element visibility. It is subjected to dynamic nature.

Fluent Wait For Automation Testing with Selenium

The Fluent wait is similar to explicit wait in terms of its functioning. In Fluent wait, you perform a Selenium wait for an element when you are not aware of the time it may take to be visible or clickable. The few differential factors that Fluent wait offers are:

  • The polling frequency- In case of Explicit wait, this polling frequency is by default 500 milliseconds. Using Fluent wait, you can change this polling frequency based on your needs, i.e you can tell your script to keep checking on an element after every ‘x’ seconds.
  • Ignore Exception- During polling, in case you do not find an element, you can ignore any exception like ‘NoSuchElement’ exception etc.

Apart from these differential factors, like Explicit wait or Implicit wait, you can define the amount of time to wait for the element to be visible or actionable. The below syntax or lines of code are used to define Fluent wait in Selenium:

The syntax appears to be complex, but once you start using, it may become handy. Probably this is one of its biggest reason where testers opt to go for explicit wait more than Fluent wait. Also, the major difference between the Explicit wait and the Fluent wait is, that the Explicit Selenium Wait provides predefined conditions, which are applied on elements we need to wait for, whereas, in case of Fluent Selenium wait, you can define your own customized conditions within the apply method.

My opinion on Fluent Selenium Wait?

I personally haven’t found any useful implementation of fluent wait in a real-time example, hence would like to refrain myself from the implementation of it, as of now. I am as eager as you, for the launch of Selenium 4 ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/what-to-expect-from-the-new-version-of-selenium-4-alpha/). I am hoping that after the launch we may get more insights on the practical advantage of using Fluent wait over the other Selenium waits. So far based on my experience, I happen to lean towards the use of Explicit Selenium wait, owing to its easier implementation of code than Fluent Selenium wait. If you feel otherwise then let me know in the comments below.

Role Of Selenium Waits For Cloud-Based Selenium Grid

Majority of testers usually prefer to perform automated website testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/common-challenges-faced-during-website-automated-testing/) using a cloud-based service provider for Selenium testing such as LambdaTest, a cross browser testing ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/) tool on cloud. Wondering why?

So what happens if you are running your automation scripts ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/a-complete-guide-for-your-first-testng-automation-script/) on a cloud-based Selenium Grid such as LambdaTest? How can Selenium waits contribute to an effective test result?

Selenium Waits: Implicit, Explicit, Fluent And Sleep

Written by  on October 15, 2019

Posted by Sadhvi Singh | August 14, 2019 Selenium Tutorial •

 

Selenium waits for page load play an important part in your Selenium scripts. They help to make them less flaky and more reliable. Selenium provides multiple waits to provide adequate wait or pause in your script execution based on certain conditions. Thereby ensuring you don’t end up getting failed scripts as you perform automation testing with Selenium ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/). In this tutorial, we will be explaining the types of Selenium waits and sleep, there real-time examples and a comparison study on them. Let us start by answering a pivotal question “Why should we use Selenium waits?”

Why Do We Need Selenium Waits?

Majority of modern application’s front-end is built on JavaScript or Ajax, using frameworks such as React, Angular, or any other which takes a certain time for the web elements to load on the page, whenever that page is loaded or refreshed. Hence, in case you tend to locate an element in your script which is yet to load on the page, selenium will throw you ‘ElementNotVisibleException’ message.

Below code snippet will help you showcase the same problem as you execute automation testing with Selenium. In this code snippet, I am using an example of easemytrip.com, where post user selects the ‘From’ and ‘To’ destination with a date of journey, the web application takes a certain time to load the required flight details. In this case, without applying wait, the user tends to book the first flight from the list. Now, since the page hasn’t loaded yet, the script failed to find the ‘book now’ button. Resulting in throwing a ‘NoSuchElementException’. Code snippet and console output below:

Console Output:

I have made use of XPath for locating web elements using the scripts for automation testing with Selenium.

Read More: Complete Guide For Using XPath In Selenium With Examples ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/complete-guide-for-using-xpath-in-selenium-with-examples/)

Selenium wait for a page to load helps in resolving this issue. There are different types of Selenium waits like Implicit wait and Explicit wait, that ensures the elements are loaded into the page before they are discovered by the Selenium script ^(http://seoblackhatforum.com/goto/https://www.lambdatest.com/blog/how-to-use-name-locator-in-selenium-automation-scripts/) for further actions.

Types of Selenium Waits For Page Load

When performing automation testing with Selenium, we use the following types of waits as we generate our Selenium script:

  • Thread.Sleep() method
  • Implicit Wait
  • Explicit Wait
  • Fluent Wait

Let us understand each one of these in-depth.

Thread.Sleep() For Automation Testing with Selenium

Sleep is a static method that belongs to the thread class. This method can be called using the reference of the class name i.e Thread. If you use Thread.sleep while performing automation testing with Selenium, then this method will stop the execution of the script for the specified duration of time, irrespective of whether the element is found or not on the web page. It accepts the time duration in milliseconds. The syntax for the same is:

Thread.sleep(3000);

The sleep function throws InterruptedException so it should be handled using a try-catch block as below:

Why Using Thread.Sleep() Is Not A Good Idea?

I will now be highlighting some disadvantages of using the thread.sleep().

  • Selenium Webdriver waits for the specified time, irrespective of the element is found or not. In case the element is found much before the specified duration, the script will still wait for the time duration to elapse, thereby increasing the execution time of the script.
  • If the element to be present does not appear after a static time and keep changing, then you will never know an estimated time needed for the sleep function. In case it takes more time than the defined time, the script shall throw an error. Which is why, if you are dealing with dynamic elements using Selenium waits then it is wise to not use Thread.sleep().
  • Thread.sleep is intended only for the element it is written prior to. In case you have two to fours elements which need to wait for a certain duration to load, you need to specify Thread.sleep as many times in that case. And if you do that! Well, you will find your script filled with Thread.sleep() syntax all over the place.

Owing to the above disadvantages, using Thread.Sleep() in your script creation is considered as a bad practice.

The below code snippet highlights the usage of Thread.Sleep() for automation testing with Selenium. In this example, we are using the same example of easemytrip above, where we will stop the thread execution, once the user clicks on search. The code works smoothly in this case, without throwing any error.