2. Custom Code Integration
1. Mobile Custom Code Integration
-
- Click on Add Custom Script
- Below Popup box will appear on the screen.
-
- Add a Test Case Name and Description.
- Add your scripts on Custom Script.
- Click on Save.
Call Custom Script
-
- This action is for Android, iOS, and Web.
- This action is used to call the custom script that you created in a custom script section.
Android Appium Custom Script:
-
- For Android we can write custom scripts in Java using Appium framework.
- We can use the “driver” object to access all methods of WebDriver class.
- Following are some examples:
Sample 1
MobileElement passwordEditText = driver.findElement(By.xpath("//android.widget.EditText[@text='Password']")); passwordEditText.sendKeys("test@123");
Sample 2
MobileElement signInButton = driver.findElement(By.xpath("//android.widget.Button[@text='Sign In']")); signInButton.click();
Sample 3
WebElement ele1 = (WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(2); WebElement ele2 = (WebElement) driver.findElementsById("com.mobeta.android.demodslv:id/drag_handle").get(5); TouchAction action = new TouchAction((MobileDriver) driver); System.out.println("It Is dragging element."); action.longPress(ele1).moveTo(ele2).release().perform();
iOS XCTest Custom Script
-
- For the iOS XCTest framework we can write custom scripts in Swift.
- We can use the “app” object to access all methods of XCUIApplication class.
- Following are some examples:
Sample 1
app.pinch(withScale: 3, velocity: 1)
Sample 2
let windowWidth = app.windows.element(boundBy: 0).frame.size.width let windowHeight = app.windows.element(boundBy: 0).frame.size.height let point1 = CGPoint(x: windowWidth/2, y: (windowHeight/2)+10) tapAtCoordinates(point: point1)
Sample 3
var element = app.staticTexts.containing(.staticText, identifier: "keyValue").element(boundBy: 0) element.tap()
Sample 4
let windowWidth = app.windows.element(boundBy: 0).frame.size.width let windowHeight = app.windows.element(boundBy: 0).frame.size.height let point1 = CGPoint(x: windowWidth/2, y: windowHeight/2) longPressAtCoordinates(point: point1, seconds: 3)
Sample 5
print(app.debugDescription)
iOS Appium Custom Script
-
- For iOS we can write custom scripts in Java using Appium framework.
- We can use the “driver” object to access all methods of WebDriver class.
- Following are some examples:
Sample 1
MobileElement passwordEditText = driver.findElement(By.xpath("//XCUIElementTypeTextView[@name=’LocationTextView’]")); passwordEditText.sendKeys("test@123");
Sample 2
MobileElement signInButton = driver.findElement(By.xpath("//XCUIElementTypeButton[@name=’Start updating’]")); signInButton.click();
Sample 3
WebElement ele1 = (WebElement) driver.findElementByAccessibilityId("drag_handle1"); WebElement ele2 = (WebElement) driver.findElementByAccessibilityId("drag_handle2"); TouchAction action = new TouchAction((MobileDriver) driver); System.out.println("It Is dragging element."); action.longPress(ele1).moveTo(ele2).release().perform();
2. Web Custom Code Integration
Web Custom Script
-
- For Web we can write custom scripts in Java using the TestNG framework.
- We can use the “driver” object to access all methods of WebDriver class.
- Following are some examples:
Sample 1
WebElement reqdate =driver.findElement(By.xpath("//input[@id='outerForm:billingCycleRequestDate_input']")); reqdate.clear(); reqdate.sendKeys(var_RequestDate);
Sample 2
WebElement planNameEleme = driver.findElement(By.id("outerForm:grid:0:planCodeTextOut2")); String planeNameStr = planNameEleme.getText().trim(); boolean isValidPlan = planeNameStr.contains("WHOLE") || planeNameStr.contains("UNIVERSAL"); Assert.assertTrue(isValidPlan,"Plan name does not contain Whole Life or Universal Life");
Sample 3
java.text.SimpleDateFormat formatter = new java.text.SimpleDateFormat("MM/dd/yyyy"); try { java.util.Date date = formatter.parse(var_PaidToDate.trim()); java.util.Calendar c = java.util.Calendar.getInstance(); c.setTime(date); c.add(java.util.Calendar.YEAR,1); java.util.Date onePlusDate = c.getTime(); var_OneYearPlusPaidToDate = formatter.format(onePlusDate); } catch (ParseException e) { System.out.println("Conversion from String to Date Failed. Please check PaidToDate format."); }
Sample 4
WebElement premimumAmountweb = driver.findElement(By.xpath("//span[@id='outerForm:grid:0:appliedAmountOut2']")); String premimumAmounttext =premimumAmountweb.getText().trim(); writeToCSV("Member Identifier", var_MemberIdentifier); writeToCSV("PAGE #: DATA TYPE", "SRP445 Applied Amount"); writeToCSV("GIAS DATA",premimumAmounttext); writeToCSV("PDF DATA", var_AmounttoAppliedPDF); writeToCSV("RESULT", (premimumAmounttext.equals(var_AmounttoAppliedPDF)) ? "PASS" : "FAIL");
java.time.LocalDate date = java.time.LocalDate.now(); java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ofPattern("MM/dd/yyyy"); String formattedDate = date.format(formatter); driver.findElement(By.xpath("//input[@type='text'][@name='outerForm:effectiveDate']")).sendKeys(formattedDate);