The testing phase of software development represents a crucial step which makes applications functional with user-friendly interfaces alongside the complete elimination of bugs. The automated testing process has become more efficient with the adoption of Selenium WebDriver and Appium which stand as the popular testing automation tools. The integrated tools assist developers along with testers in performing automated testing of mobile and web applications thereby enhancing their efficiency and reliability. This guide gives an extensive explanation for producing test scripts through Selenium WebDriver and Appium libraries, including Selenium mobile testing.
Introduction
Let’s begin with an introduction to Selenium WebDriver and Appium, followed by a description of test scripts:
- An open-source automation tool named Selenium WebDriver allows users to control web browsers through automated operations. Through its code-writing capability, developers and testers can interact with web pages to verify their behaviour. Selenium WebDriver finds widespread popularity because its development supports programming languages such as Java, Python, C# and JavaScript.
- The open-source automation tool Appium serves as a platform to control mobile applications. This tool operates on Android and iOS systems simultaneously while testers can create tests which reflect their familiar Selenium codebase. The design of Appium supports cross-platform controls since tests developed for a single platform can work on other platforms.
Both integrated tools let automated testing happen simultaneously across web and mobile applications within a single workflow. The following part demonstrates the steps for creating test scripts through these two libraries.
Prerequisites
There are three essential components you need to install for Selenium WebDriver and Appium test script development on your computer system:
- Java Development Kit (JDK): A Java Development Kit (JDK) is required to write and execute Java-based test scripts.
- Eclipse/IntelliJ IDEA: It represents the selection of IDEs designed for Java application development.
- Maven: Build an automation tool that helps manage dependencies for Selenium and Appium.
- Appium Server: Used to interact with mobile devices during automation.
- Android Studio/Xcode: Required for testing mobile applications on Android and iOS devices, respectively.
- Selenium WebDriver: The library that you will use for browser automation.
- Appium Client Libraries: The library containing the code for browser automation will be used here.
Additionally, you need to have either Chrome or Firefox browser at your disposal, along with physical or virtual mobile equipment for tests.
Setting Up the Environment
Let’s see how:
1. Install Selenium WebDriver
The first step requires adding Selenium WebDriver to your application project. You can include Maven dependencies in the pom.xml file to accomplish this task. For example:
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
</dependencies>
After adding the dependency, Maven will automatically download the Selenium WebDriver libraries for you.
2. Install Appium
Similarly, you can add the Appium client library to your project by modifying your pom.xml file:
<dependencies>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>7.6.0</version>
</dependency>
</dependencies>
Apart from the Appium server setup, you also need to obtain the Appium server while you have the option to run it through the command line interface or service mode. The official website of Appium contains all installation instructions.
3. Setup Android and iOS Testing Environments
Your mobile testing needs either Android Studio or Xcode based on testing Android or iOS applications.
- To execute Android tests, you have to establish an Android Virtual Device (AVD) and install SDK tools that enable testing either virtual or physical devices.
- To establish iOS testing you must install both Xcode together with the proper device settings.
Writing Test Scripts with Selenium WebDriver
We should initiate test script development for web application automation through Selenium WebDriver.
1. Basic WebDriver Test Script
We should start by creating a straightforward test script which opens web pages and manipulates element interactions.
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
public class WebDriverTest {
public static void main(String[] args) {
// Set the path for the ChromeDriver executable
System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);
// Initialize the WebDriver (Chrome)
WebDriver driver = new ChromeDriver();
// Open a website
driver.get(“https://www.example.com”);
// Find an element by ID and click it
driver.findElement(By.id(“loginButton”)).click();
// Close the browser
driver.quit();
}
}
This script performs the following actions:
- Set up the ChromeDriver to drive the Chrome browser.
- Opens a specific webpage (https://www.example.com).
- Locates the login button by its ID and clicks it.
- Finally, it quits the browser.
The demonstration shows the basic automation capabilities of Selenium WebDriver when it comes to browser control.
Advanced WebDriver Test Script with Assertions
Tests can demonstrate proper functioning by including assertions to confirm outcome correspondences with predicted behaviors:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.junit.Assert;
public class WebDriverAdvancedTest {
public static void main(String[] args) {
System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”);
WebDriver driver = new ChromeDriver();
driver.get(“https://www.example.com”);
// Find an element by ID
WebElement loginButton = driver.findElement(By.id(“loginButton”));
// Assert that the login button is displayed
Assert.assertTrue(“Login button is not displayed”, loginButton.isDisplayed());
driver.quit();
}
}
This sample code implements JUnit’s Assert class to validate the presence of the login button on the displayed page. The test execution will generate a failure result in case the button does not appear on the page.
Writing Test Scripts with Appium
Mobile automation tool Appium exists to serve specific mobile devices through its capabilities that work on iOS and Android systems simultaneously. This section will explain a basic test script written for the Android platform using Appium.
1. Basic Appium Test Script for Android
When testing Android you need to establish desired capabilities which indicate the exact device information along with your testing application features. This Appium test script demonstrates the following format:
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class AppiumTest {
public static void main(String[] args) throws Exception {
// Set the desired capabilities
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“deviceName”, “Android Emulator”);
capabilities.setCapability(“platformName”, “Android”);
capabilities.setCapability(“app”, “/path/to/app.apk”);
// Initialize the AndroidDriver
AndroidDriver<AndroidElement> driver = new AndroidDriver<>(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
// Engage with an application element
AndroidElement loginButton = driver.findElement(By.id(“com.example:id/loginButton”));
loginButton.click();
// Close the app
driver.quit();
}
}
This script performs the following actions:
- Sets up desired capabilities for the Android device (or emulator) and specifies the APK file for the app under test.
- Initializes the Appium driver.
- Locates and clicks the app’s login button.
- Closes the app after the test.
2. Appium Test Script for iOS
Writing tests for iOS is very similar to writing tests for Android, with slight differences in capabilities. Here’s an iOS example:
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.ios.IOSElement;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class AppiumIOSAppTest {
public static void main(String[] args) throws Exception {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“deviceName”, “iPhone Simulator”);
capabilities.setCapability(“platformName”, “iOS”);
capabilities.setCapability(“platformVersion”, “14.4”);
capabilities.setCapability(“app”, “/path/to/ios-app.app”);
// Initialize the iOSDriver
IOSDriver<IOSElement> driver = new IOSDriver<>(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
// Locate and click an element
IOSElement loginButton = driver.findElement(By.id(“com.example.ios:id/loginButton”));
loginButton.click();
driver.quit();
}
}
The script maintains its structure from the Android version yet operates to examine iOS devices.
The power of Selenium WebDriver and Appium tools fails to address the excessive complexity that occurs during testing between various browsers and devices while maintaining your testing infrastructure. For example, LambdaTeststands out as a powerful solution for users’ needs. LambdaTest enables users to perform automated tests on real mobile devices, ensuring that applications pass a mobile friendly test, while also performing browser compatibility checks.
Through its cloud-based system LambdaTest enables users to execute Selenium and Appium tests on a scalable collection of real browser systems in addition to real devices without demanding any physical infrastructure or maintenance responsibilities. This platform enables users to perform automated testing for web applications and mobile applications where tests run on real Android and iOS devices.
How LambdaTest Integrates with Selenium WebDriver and Appium
LambdaTest makes it easy to execute your Selenium WebDriver and Appium tests remotely. You simply configure your test scripts to point to LambdaTest’s cloud grid URL. The following shows you how to execute Selenium WebDriver tests on LambdaTest:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“browserName”, “Chrome”);
capabilities.setCapability(“platform”, “Windows 10”);
String gridURL = “https://username:[email protected]/wd/hub”;
WebDriver driver = new RemoteWebDriver(new URL(gridURL), capabilities);
driver.get(“https://www.example.com”);
driver.quit();
You may conduct tests on real mobile devices with Appium using LambdaTest:
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“platformName”, “Android”);
capabilities.setCapability(“deviceName”, “Samsung Galaxy S10”);
String gridURL = “https://username:[email protected]/wd/hub”;
AndroidDriver<MobileElement> driver = new AndroidDriver<>(new URL(gridURL), capabilities);
driver.findElement(By.id(“com.example:id/loginButton”)).click();
driver.quit();
Benefits of LambdaTest
- Cross-browser testing: Run Selenium tests on multiple browsers and versions.
- Real device testing: Mobile applications receive evaluation on real iOS and Android operating systems through the use of Appium.
- Scalability: The test process becomes faster while test coverage expands when tests are executed simultaneously.
- Instant feedback: You receive instant logs and videos in combination with screenshots that help you detect problems in real-time.
Combining Selenium and Appium
The testing of web applications in parallel with mobile applications becomes necessary in typical operational situations. Test scripts across platforms become possible by using Selenium WebDriver together with Appium. Mobile web app tests run through Appium, whereas regular web app tests run through Selenium WebDriver in the same test suite.
// Selenium WebDriver for web testing
WebDriver webDriver = new ChromeDriver();
webDriver.get(“https://www.example.com”);
// Appium driver for mobile testing
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(“deviceName”, “Android Emulator”);
AndroidDriver<AndroidElement> appiumDriver = new AndroidDriver<>(new URL(“http://127.0.0.1:4723/wd/hub”), capabilities);
Adopting this approach enables you to produce combined test scripts which support web applications together with mobile platforms thus enabling comprehensive coverage.
In Conclusion
The software development lifecycle now heavily depends on automated testing, which lets developers and testers verify the quality of web applications and mobile applications together. Selenium WebDriver and Appium offer robust, open-source frameworks that streamline the testing process for web and mobile platforms, respectively. The tools grant testers the ability to write tests across multiple platforms together with automation capabilities for browsers and simple mobile application verification tests.
Testers would benefit from running complete test suites through parallel execution when they combine Selenium WebDriver for web applications with Appium for mobile apps. The combined approach leads to shorter test durations together with greater test scope achievement. The cloud-based solution LambdaTest provides testers with real browsers and device access along with infrastructure-free testing that enhances scalability and efficiency for the entire process.
Selenium and Appium work together as a leading toolkit, allowing users to achieve automated testing of simple web applications and complex mobile apps. The ongoing increase in high-quality cross-platform application demand requires individuals to master these tools in order to deliver software which meets user requirements.