How to Login and Do Multiple Test Cases Selenium Java
Execution of automation testing requires a comprehensive understanding of numerous automation tools and frameworks. Among these tools, Selenium is the well-nigh popular due to its ease of use and relevant features. Selenium is used to automate tests on websites to monitor their operation.
A key feature of many websites is their login function. Automating the testing of this characteristic is simple plenty when using Selenium WebDriver, every bit this article volition demonstrate.
Let's go started.
Selenium WebDriver allows testers to choose a programming language to create test scripts. Information technology cannot handle window components, but this drawback can be overcome past using tools similar Sikuli, Auto IT, etc.
Carry in mind that Selenium tests must be run on real browsers and devices for accurate results. Start running tests on 3000+ real browsers and devices on BrowserStack's existent device cloud. Run parallel tests on a Cloud Selenium Grid to go faster results without compromising on accuracy. Testers can also conduct Cypress testing on 30+ real browser versions beyond Windows and macOS. Notice bugs before users exercise past testing software in existent user conditions with BrowserStack.
Run Selenium Tests on Existent Browsers Complimentary
Prerequisites for Login Automation using Selenium Webdriver
- Download and Install JDK(Java Development Kit)
- Install Eclipse from the official website
- Download the Selenium Java Client version
- Configure the drivers depending on the browser. The example hither will be using a chrome driver for Chrome
Steps for Login Automation using Selenium WebDriver
Before performing automation testing for the login functionality, at that place are a couple of basic steps that need to be followed for the test instance to be written:
- Create a Selenium WebDriver case
- Configure browser if required
- Navigate to the required web page
- Locate the relevant web element
- Perform activeness on the web chemical element
- Verify and validate the activity
Now let'due south walk through each of these steps in detail.
1. Create a Selenium WebDriver case
To launch the website in the desired browser, set up the system properties to the path of the commuter for the required browser. This example will use ChromeDriver for Login Automation using Selenium Webdriver. The syntax for the same will be:
Webdriver driver = new ChromeDriver(); Arrangement.setProperty("webdriver.chrome.driver", "Path of the chrome driver");
ii. Configure the Web browser
Usually, the spider web page will be in a minimized format when the test case is run. Maximize the browser for a clear picture show of the test cases executed. Use the control beneath to do the same:
commuter.manage.window.maximize();
three. Navigate to the spider web URL
Open the browser with the desired URL. Use the command below to open the URL in the desired instantiated browser:
driver.get("https://www.browserstack.com/users/sign_in");
iv. Locating the Web Chemical element
Locators are an essential part of every Selenium script as they identify the elements that the test script will interact with to replicate user actions.
For example, let's attempt to locate the electronic mail and countersign field of the login course of Browserstack sign in page.
Inspect the email field to locate the element using the ID locator, as shown in the image below:
Locate it via the ID locator in Selenium WebDriver:
driver.findElement(By.id("user_email_login"));
Since this returns a webelement, store it in webelement variable with:
WebElement username=driver.findElement(By.id("user_email_login"));
Echo the same steps for the password field.
driver.findElement(Past.id("user_password")); WebElement password=driver.findElement(By.id("user_password"));
Run Selenium Tests for Free
five. Perform Activity on the Located Spider web Chemical element
After locating the element, testers demand to perform the desired activity. In this case, the activity is entering text in the electronic mail and countersign fields and striking the login button. For this, the script uses sendKeys and click methods, equally shown beneath:
username.sendKeys("abc@gmail.com"); password.sendKeys("your_password"); login.click();
6. Verify & Validate The Activeness
To validate the results, use assertion. Assertions are important for comparison the expected results to the bodily results. If it matches, the test example passes. If non, then the test case fails. The syntax below will help to assert (validate) the effect from deportment by performing login automation:
Assert.assertEquals(Cord actual, String expected);
Salvage the actual URL post-login into a string value, which is:
Cord actualUrl="https://www.browserstack.com/users/sign_in";
The Expected URL can be identified by using the method below:
String expectedUrl= driver.getCurrentUrl();
The concluding exclamation would await like:
Assert.assertEquals(actualUrl, expectedUrl);
If the test case is passed, it will retrieve the aforementioned. Else it volition return as failed.
Note: Testers need to use JUnit or TestNG to cheque for the examination case condition.
Given below is the full selenium code for automating login page in chrome using Selenium WebDriver:
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.chrome.ChromeDriver; import org.testng.Assert; import org.testng.annotations.Examination; public class LoginAutomation { @Test public void login() { Organization.setProperty("webdriver.chrome.commuter", "path of commuter"); WebDriver driver=new ChromeDriver(); driver.manage().window().maximize(); commuter.get("https://www.browserstack.com/users/sign_in"); WebElement username=driver.findElement(By.id("user_email_Login")); WebElement countersign=driver.findElement(By.id("user_password")); WebElement login=driver.findElement(By.proper noun("commit")); username.sendKeys("abc@gmail.com"); countersign.sendKeys("your_password"); login.click(); String actualUrl="https://live.browserstack.com/dashboard"; String expectedUrl= driver.getCurrentUrl(); Affirm.assertEquals(expectedUrl,actualUrl); } }
On executing the code, Selenium will navigate to the Chrome browser and open the Browserstack login page. Then, information technology will log in using the relevant credentials. It will as well check the test case status using Assert and try to match the URL.
Follow the steps and protocol detailed above to automate the login role of websites with Selenium. Remember to leverage the power of existent browsers and devices along with Selenium's many abilities to create immediately effective test scripts that generate desirable results with minimal time and effort.
How to Login and Do Multiple Test Cases Selenium Java
DOWNLOAD HERE
Source: https://www.browserstack.com/guide/login-automation-using-selenium-webdriver
Posted by: 7newsonlinetty.blogspot.com
comment 0 comments
more_vert