URL Class in Appium Testin -Notes By ShariqSP
Understanding the URL Class in Appium Testing
The URL class in Java is part of the java.net
package and is frequently used in Appium testing to establish a connection between the test script and the Appium server. The URL
object specifies the address where the Appium server is running and listens for WebDriver commands.
Role of the URL Class in Appium
-
Connecting to the Appium Server:
The URL class helps define the endpoint for the Appium server, typically in the format:
http://hostname:port/wd/hub
. This is required when initializing theAppiumDriver
orRemoteWebDriver
. -
Providing Server Details:
The URL contains protocol information (e.g., HTTP), host details (e.g., localhost), and the port number on which the Appium server is running (e.g., 4723 by default).
Creating a URL Object
The URL
class constructor is used to instantiate a URL object in Java. Syntax:
// Example of URL instantiation
URL serverUrl = new URL("http://127.0.0.1:4723/wd/hub");
Methods of the URL Class
The URL class provides various methods to manipulate and retrieve URL components:
-
getProtocol()
Returns the protocol used in the URL (e.g., HTTP).
URL serverUrl = new URL("http://127.0.0.1:4723/wd/hub"); System.out.println(serverUrl.getProtocol()); // Output: http
-
getHost()
Returns the host name or IP address specified in the URL.
URL serverUrl = new URL("http://127.0.0.1:4723/wd/hub"); System.out.println(serverUrl.getHost()); // Output: 127.0.0.1
-
getPort()
Returns the port number specified in the URL. If no port is specified, it returns
-1
.URL serverUrl = new URL("http://127.0.0.1:4723/wd/hub"); System.out.println(serverUrl.getPort()); // Output: 4723
-
getPath()
Returns the file or path portion of the URL.
URL serverUrl = new URL("http://127.0.0.1:4723/wd/hub"); System.out.println(serverUrl.getPath()); // Output: /wd/hub
-
toString()
Returns the complete URL as a string.
URL serverUrl = new URL("http://127.0.0.1:4723/wd/hub"); System.out.println(serverUrl.toString()); // Output: http://127.0.0.1:4723/wd/hub
Usage in Appium Testing
In Appium testing, the URL object is passed to the constructor of the AppiumDriver
, AndroidDriver
, or IOSDriver
. This establishes a connection to the Appium server and allows the driver to execute commands on the mobile device.
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.URL;
public class AppiumTest {
public static void main(String[] args) throws Exception {
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("platformName", "Android");
caps.setCapability("deviceName", "Pixel_4");
caps.setCapability("automationName", "UiAutomator2");
caps.setCapability("app", "/path/to/app.apk");
// URL to connect to the Appium server
URL serverUrl = new URL("http://127.0.0.1:4723/wd/hub");
// Create AndroidDriver instance
AppiumDriver driver = new AndroidDriver<>(serverUrl, caps);
// Interact with the app
driver.quit();
}
}
Benefits of the URL Class in Appium
- Enables flexible configuration of the Appium server address.
- Provides methods to validate and manipulate the server URL.
- Ensures seamless communication between test scripts and the Appium server.