Integrating Custom Selenium Code & Scriptless
Selenium Custom Scripts
- Click on Add Custom Script
- Below the Popup box will appear on the screen.
- Add a Test Case Name and Description.
- Add your scripts on Custom Script.
- Click on Save.
Actions
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.
Selenium Custom Script
-
- For the Web, we can write custom scripts in Java using the TestNG framework.
- We can use the “driver” object to access all methods of the 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);
As simple as that! Happy Testing