CrossBrowserTesting.com

A Design, Development, Testing Blog

  • Product Overview
  • Pricing
  • Back Home
  • Free Trial
  • Design
  • Development
  • Manual Testing
  • Test Automation
  • Visual Testing

Selenium Academy 101 FAQ

August 7, 2019 By Nick Brown Leave a Comment

Selenium Academy 101 FAQ

With the conclusion of our very first Selenium Academy class we wanted to ensure we armed you with the answers to some of the most common questions that came up during our live QA session!

In our Selenium 101 class we started at the very beginning by explaining what Selenium is, why you will want to use Selenium for your UI testing and especially why you will want to use Selenium with CrossBrowserTesting. But let’s take a look at some of the most common questions that came up during last week’s webinar.

What is the difference between a Selenium grid and CrossBrowserTesting?

While you can make your own Selenium grid with a costly device lab or VMs, many have found moving their Selenium grid to a third-party cloud like CrossBrowserTesting is faster and more productive, allowing more tests to run on more machines in less time. With over 2050 browsers to choose from, the possibilities of parallel test execution with CrossBrowserTesting are almost limitless. Our browser, device, and operating system selection includes all the latest configurations as well as legacy browsers so you can test what your users are on without worrying about any of the associated costs or upkeep. There are certain real costs of building an in-house environment as well as countless other secondary costs of not going with a cloud provider, like CrossBrowserTesting.

Selenium is open source right, why would we need to buy CrossBrowserTesting?

We can give you 8 good reasons!

  1. We make it easier to get started.
  2. Just install Selenium bindings for your programming language of choice and be done!
  3. Eliminate the work of managing and maintaining Selenium Servers, WebDrivers, and Browsers
  4. Screenshots, videos, and even network data can be captured during a Selenium test and shared with anyone
  5. Higher concurrency means faster execution
  6. Run dozens of tests at once without having to run dozens of RAM-hungry browsers
  7. Spend less time waiting and more time doing what matters
  8. Instant access to thousands of browsers and devices, including mobiles

What languages are supported with Selenium?

C#, Java, JavaScript, PHP, Python and Ruby

Which language is the easiest to get started with?

When talking with users about what language is easiest to get started with, we always recommend Python. The reason we recommend it for beginners is because it reads like English and tends to be easier for users to understand when getting started. Take a look at our blog on the Best Programming Language to Learn First.

What’s the best way to set up tests to run within a build pipeline using Jenkins?

There are only two things you need to be able to run your selenium tests from a Jenkins pipeline. You need a shell script or batch file that Jenkins can call to start your test, and Jenkins needs access to any libraries or modules that you use during the test – including the Selenium language bindings. If you are using Python, you can use VirtualEnv to create a virtual environment for Python with Selenium already installed. If you are using Java, make sure to include Selenium in your pom.xml file and run mvn build.

Does Selenium have the ability to open two webpages at the same time?

Yes, Selenium can be used to easily manage multiple pages at the same time. You can use the built-in command “switch_to_window()” documented here: Learn More Here

Is it recommended to use find element by xpath always?

Quite the opposite. Using Xpath to find elements, especially absolute Xpaths, tends to lead to very brittle tests. Any small change in the hierarchy around the element you are locating could cause the wrong element to be selected. Those bugs in your Script can sometimes be hard to debug. You should save yourself the trouble and use element IDs as much as possible

So, find_element_by_id is the best function?

Yes, it’s the most resilient to change and the easiest to use. If you can, add human-understandable IDs to any elements of your page that you want to automate.

For AngularJS based applications, do you have any specific built in functionality?

CrossBrowserTesting does not provide specific functionality to test websites built with AngularJS, but CrossBrowserTesting does work with Protractor – a Selenium-based framework created by Google for testing AngularJS. We have a short guide on getting started here: Help Doc for Protractor

How well does protractor work with just regular Angular?

It works great! That’s what it’s made for. If you run into any problems, feel free to reach out to our support team and they’ll help get you up and running.

Want to register for our 201 class on August 27th? Register now!

Filed Under: Test Automation Tagged With: automated testing, automation, FAQ, Selenium, selenium 101

Selenium 101: Managing Multiple Tabs

April 2, 2018 By Alex McPeak Leave a Comment

how to handle multiple tabs in selenium webdriver

multi-tab testing selenium

As we’ve discovered through this Selenium 101 Series, automation can be challenging when your tests require more complex actions such as testing in two windows at once or closing a pop-up. Luckily, once you know the right commands, you can begin creating scripts that reach all parts of the application you need to check.

Just like you might open web pages in different tabs locally, it’s also possible to have multiple tabs up in one browser during a Selenium test. Multi-tab testing is a common annoyance for testers who are learning Selenium — how do you open more than one tab in a single browser window, then switch back and forth between them? Many pages do this on their own. For instance, any anchor tag with an attribute like target=”_blank” will open the link in a new tab. How do we handle this in our automated tests? The good news is that it’s not as hard as it seems.

With multi-tab testing, you might want to:

  • Launch your browser
  • Maximize your browser
  • Launch multiple tabs
  • Switch between the tabs
  • Perform actions on each tab
  • Close the browser

To first open a new tab (if one doesn’t open automatically when clicking a link), you’ll use driver.findElement(By.cssSelector(“body”)). followed by sendKeys(Keys.CONTROL+”t”); You can probably already tell that we’re focusing on our webpage, then sending ctrl + t to open a new tab the way you might normally in your browser.

Any time you switch between tabs, you’ll want to use the driver.switchTo().window(<some window>); method. This allows you to change control from one tab to another. The switchTo method is also how you handle alerts, so the steps will be very similar.

You might want to throw some waits in there for good measure, but it should look something like this:

WebDriver driver = new FirefoxDriver(); 
driver.manage().window().maximize(); 
driver.get("URL");

String newWindow = driver.getWindowHandle();

driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL+”t”);
driver.get("Second URL");


//Perform whatever actions you want done on the secondary tab, let’s pretend we’re logging in somewhere

driver.findElementById(“ID”).sendKeys(“username”);
driver.findElementById(“ID”).sendKeys(“password”);
driver.findElementById(“submit”).click();

driver.switchTo().window(mainWindow);

//Perform whatever actions you want done on the main tab, we’ll click a button
driver.findElementById(“button”).click();

# Close current tab
driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL + 'w');

driver.close();


This script will give you a good foundation to use in future tests where you might want to switch tabs. For more in our Selenium 101 Series, check out our previous posts:

  • Selenium 101: How to Automate Your Login Proces
  • Selenium 101: Running Your Test Against Two Browsers at Once
  • Selenium 101: Navigating Waits and Sleeps
  • Selenium 101: How to Take Screenshots During Automated Tests
  • Selenium 101: Automating User Actions
  • Selenium 101: Generating Test Reports
  • Selenium 101: Mastering Selenium Design Pattern

 

Filed Under: how-to, Selenium, Test Automation Tagged With: how to, multiple tabs, Selenium, selenium 101, test automation

Selenium 101: Generating Test Reports

March 12, 2018 By Alex McPeak Leave a Comment

selenium report generation extent reports allure testng junit

selenium report generation extent reports allure testng junit
Even the most thorough Selenium testing scripts will prove insufficient without helpful reporting to match. Without reporting, all the hard work that you put into writing and executing your test cases will be lost when there’s no way to track the information and communicate it with others on your team.

Selenium doesn’t support report generation, but it’s easy enough to do with the right tools. Once you become accustomed to good automation and reporting practices, you’re on track for being a Selenium master, which is why we have the methods to get you there.

TestNG

TestNG is a unit testing framework with a library that includes a default reporting feature to generate a test output reports to a folder. The nice thing about this is that you don’t have to write any scripts or additional code to get the reports.

You also have the option of a detailed report or summary report, depending on which you prefer. A file called index.html contains a complete report generated automatically with information for errors, groups, run-times, and reporter logs.

The emailable-report.html is more of a summary that shows the scores for each test case that can be embedded and shared easily.

You can also use the ITestListener Interface, which is probably the most popular choice, or IReporter to further customize TestNG reports.

JUnit

JUnit is another way to generate reports with Selenium using HTML. Using the TestWatcher class, it calls “succeeded” or “failed” methods to follow predefined behavior based on your tests outcome.

Using JUnit annotations @BeforeClass and @AfterClass will help define your pre/post-test tasks including opening the HTML report, and closing out of the report, respectively.

Like TestNG, JUnit won’t give you anything fancy, but it’s a practical way to generate reports and it’s a popular option for teams.

Extent Library

Similar to JUnit, Extent Library generates HTML reports, but Extent Library has more features and capabilities that make reporting a little bit more readable and nicer to look at with pie charts and other visuals.

The <Extent Report> library has many added capabilities for generating extensive test report including logs, filters, interactivity, parallel execution, execution history, and the ability to attach screenshots.

This is probably one of the most feature-rich ways to generate a report with Selenium, and it can be used with JUnit and TestNG. All you need to do is create a testNg.xml file then click Run As TestNG suite and Extent Report will automatically generate an HTML report.

Allure

Allure is actually a framework that also serves as a flexible, lightweight report tool allowing you to add steps, attachments, and parameters. It does this through an adapter that saves test information to XML files and then making them into an HTML report.

Allure is definitely the hidden gem of open source test reporting tools. This is another great option for creating a report that actually looks good while conveying information since it organizes aspects like timelines, graphs, and defects for you.

Do It Yourself

Making your own test report is a perfectly good way to go about it, and although you have to manually find and record the information yourself, it will allow individual customization. The best way to do this is probably creating a PDF with Java API IText since it will be easily shareable.

You may want to record all tests, or just failing tests, but either way, you should make sure you include class, method, and exception. Additionally, include the cause of test failures and attach supporting evidence of those results.

The best way to do this is by using screenshots from failing Selenium tests. Linking to them works just fine, but you could also put them directly into the document. This will ensure that the details of any errors are accounted for by whoever reads the report.

Other useful information might be the percentage of pass and fail, error severity, test execution times, and details on test environments. You want to make the report as detailed as possible without putting more information than necessary.

To learn more about the fundamentals of test automation, check out the rest of our Selenium 101 series:

  • Selenium 101: How to Automate Your Login Process
  • Selenium 101: Running Your Test Against Two Browsers at Once
  • Selenium 101: Navigating Waits and Sleeps
  • Selenium 101: How to Take Screenshots During Automated Tests
  • Selenium 101: Automating User Actions
  • Selenium 101: Generating Test Reports
  • Selenium 101: Mastering Selenium Design Pattern
  • Selenium 101: Managing Multiple Tabs

Filed Under: Selenium Tagged With: reporting, Selenium, selenium 101, test automation

Selenium 101: How To Automate Your Login Process

January 31, 2018 By Alex McPeak 1 Comment

automate login with selenium

automate login with selenium
While learning Selenium can surely be challenging in the shift from manual to automation, starting small and making the effort to be continuously learning will help you become proficient in no time.

CrossBrowserTesting wants to help your team get started with automated testing, which is why we’re creating Selenium 101 guides to teach you the basics. By the end, every software team will want you scripting tests for them.

For the first of the series, we’re starting at the very beginning by showing you how to automate a simple login process with Selenium using Python (because it’s one of the easiest programming languages to learn first).

You’ll want to install Python, Chrome Driver, and Selenium before starting. ChromeDriver will come in the form of an executable (Windows) or a binary (Mac/Unix). Those technical details aren’t too important now, but you’ll need the file itself. You can get the latest release of ChromeDriver here. Use the following command to add the Selenium library to Python.

pip install selenium

You’ll also need to import some specific modules from Python’s Selenium library. At the bare minimum, you’ll need to do the following:

from selenium import webdriver

Finally, we need to actually start a webdriver. We can do so with only one line of code:

# if chromedriver is not in your path, you’ll need to add it here

driver = webdriver.Chrome()

Now, we can get started automating a simple task like your website’s login form. Basically what we want to do is navigate to the website, locate the username and password fields, enter your credentials, and submit them to get past your login screen. You might also want to define a “method” (something you can call repeatedly) so you can reuse it within other tests. It’ll look something like this:

def site_login():
driver.get (“URL”)
driver.find_element_by_id(“ID”).send_keys(“username”)
driver.find_element_by_id (“ID”).send_keys(“password”)
driver.find_element_by_id(“submit”).click()

For example, if we were to be automating a Facebook login, it would look something like this:

def fb_login():
driver.get (“https://www.facebook.com”)

driver.find_element_by_id(“email”).send_keys(‘fakeemail@crossbrowsertesting.com’)
driver.find_element_by_id(“pass”).send_keys(“fakepassword1”)
driver.find_element_by_id(“loginbutton”).click()

There’s more than one way to locate the elements of your web application in order to find the username and password fields, and some may not always be available to you depending on the way your webpage was written. You can find elements by ID, as we exemplified, but you can also locate them by name, XPath, CSS Selectors, and more. To read about the different ways to locate elements during your login process, read Selenium’s official documentation.

Some pages use dynamic content (meaning lots of JavaScript!). To handle this effectively, we sometimes need to wait for some event to occur. To check and make sure that the login was successful, you might want locate an element on the page you’d land on after your login form by using a wait. We’ll need a few more components from the Selenium library. With the same example, you could do it by locating an element by like this:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait WebDriverWait(driver, 10).until(EC.title_contains("home"))

The above code will wait a maximum of 10 seconds while attempting to find the “Home” button displayed when you first login to Facebook. Again, there are a few different ways to go about this including waiting for an element to be clickable, visible, or present on the page. You can read more about that here.

There you have it; this should provide a basic foundation for automating a simple login process with Selenium in Python. Take a look at the rest of our Selenium 101 series to continue on your journey to becoming a test automation master:

  • Selenium 101: Running Your Test Against Two Browsers at Once
  • Selenium 101: Navigating Waits and Sleeps
  • Selenium 101: How to Take Screenshots During Automated Tests
  • Selenium 101: Automating User Actions
  • Selenium 101: Generating Reports
  • Selenium 101: Mastering Selenium Design Patterns
  • Selenium 101: Managing Multiple Tabs

Filed Under: Test Automation Tagged With: login, selenium 101, test automation

Try CrossBrowserTesting

Everything you need for testing on the web. Browsers & Devices included.


  • Grid In The Cloud
  • Simple CI Integrations
  • Native Debugging Tools
  • Real iOS and Android
  • 2050+ Browser/OS Combinations
  • Intuitive REST API

Start Testing Today

Want Testing Tips?

Want the latest tips & strategies from industry experts right to your inbox? Sign up below.
 

Join Over 600,000 Testers & Developers And Start Testing Today

Learn more Free Trial

Features

  • Live Testing
  • Selenium Automation
  • Automated Screenshots
  • Screenshot Comparison
  • Local Testing
  • Real Devices

Solutions

  • Automated Testing
  • Visual Testing
  • Manual Testing
  • Enterprise
  • Internet Explorer

Resources

  • Browsers & Devices
  • Blog
  • Webinars
  • Integrations
  • ROI Calculator

Company

  • About Us
  • Careers
  • Plans
  • Terms of use
  • Security

Support

  • Help Center
  • API Docs
  • Schedule A Demo
  • Contact Us
  • Write For Us