Skip to content

Commit

Permalink
Update_Freamwork_29-07-24
Browse files Browse the repository at this point in the history
Update_Freamwork_29-07-24

1. Change Login Cread from json for mobile and web
2. Add thread.sleep method
3. Add try-catch in action class
4. Create Wait Action Class
5. Add Driver Quite Method
6. Change APK path
  • Loading branch information
teamqaqacraft committed Jul 29, 2024
1 parent 07b6b29 commit cf00335
Show file tree
Hide file tree
Showing 23 changed files with 1,000 additions and 661 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"automationName": "UiAutomator2",
"platformName": "Android",
"platformVersion": "12",
"deviceName": "RMX1825",
"udid": "PRY5AUOJONAAVO6T",
"platformVersion": "14",
"deviceName": "SM A546E",
"udid": "RZCW31N918R",
"appPackage": "com.bromcom.mcas",
"appActivity": "crc64738b383418757493.MainActivity",
"app": "${apkPath}",
Expand Down
7 changes: 7 additions & 0 deletions data/mobile/MobileLoginCredentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"email_id" : "[email protected]",
"password" : "Br0mc0m_1234",
"student_profile_name" : "Maya ABIY",
"Pin" : "12345"

}
3 changes: 3 additions & 0 deletions data/web/GroupNamePrefix.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"prefix" : "QA_GROUP_"
}
6 changes: 6 additions & 0 deletions data/web/LoginCredentials.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"url" : "https://mis-regression.bromcom.dev/Nucleus/Framework/Login.aspx",
"school_id" : "711124",
"username" : "brcm",
"password" : "Br0mc0m_1234"
}
311 changes: 311 additions & 0 deletions src/test/java/actions/MobileElementAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,311 @@
package actions;

import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.MobileElement;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.PageFactory;

public class MobileElementAction {

private AppiumDriver<MobileElement> driver;


public MobileElementAction(AppiumDriver<MobileElement> driver) {
this.driver = driver;
PageFactory.initElements(new AppiumFieldDecorator(driver), this);
}

// Scrolls the application view to find an element with the specified text.
public static MobileElement scrollToElementByText(AppiumDriver<MobileElement> driver, String elementText) {
try {
return driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().scrollable(true).instance(0))"
+ ".scrollIntoView(new UiSelector().text(\"" + elementText + "\"))"));
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Element Not Found with text: " + elementText);
}
}

// Method to click on an element by different locator types
public static void clickElementByLocators(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue) {

MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsClickable(element);
element.click();
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Element Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to double-click on an element by different locator types
public static void doubleClickElementByLocator(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue) {
MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsClickable(element);
Actions actions = new Actions(driver);
actions.doubleClick(element).perform();
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Element Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to send key on an element by different locator types
public static void sendKeysToMobileByLocator(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue, String text) {
MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsVisible(element);
element.sendKeys(text);
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Element Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to Select Dropdown
public static void selectMobileDropdownByVisibleText(AppiumDriver<MobileElement> driver, MobileElement dropdownElement, String visibleText) {
try {
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsClickable(dropdownElement);
dropdownElement.click();
MobileElement option = driver.findElement(By.xpath("//android.widget.CheckedTextView[@text='" + visibleText + "']"));
option.click();
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Dropdown or option Not Found with text: " + visibleText);
}
}

// Method to Check Checkbox by different locator types
public static void mobileCheckCheckbox(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue) {
MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsVisible(element);
if (!element.isSelected()) {
element.click();
}
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Checkbox Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to Uncheck Checkbox by different locator types
public static void mobileUncheckCheckbox(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue) {
MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsClickable(element);
if (element.isSelected()) {
element.click();
}
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Checkbox Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to click on an RadioButton by different locator types
public static void mobileCheckRadioButton(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue) {
MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsClickable(element);
if (!element.isSelected()) {
element.click();
}
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Radio Button Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to click on an RadioButton by different locator types
public static void mobileUncheckRadioButton(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue) {
MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsClickable(element);
if (element.isSelected()) {
element.click();
}
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Radio Button Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to Click on ToggleButton on an element by different locator types
public static void mobileToggleButton(AppiumDriver<MobileElement> driver, String locatorType, String locatorValue, boolean turnOn) {
MobileElement element;
try {
switch (locatorType.toLowerCase()) {
case "text":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().text(\"" + locatorValue + "\")"));
break;
case "resourceid":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().resourceId(\"" + locatorValue + "\")"));
break;
case "classname":
element = driver.findElement(MobileBy.AndroidUIAutomator(
"new UiSelector().className(\"" + locatorValue + "\")"));
break;
case "xpath":
element = driver.findElement(MobileBy.xpath(locatorValue));
break;
default:
throw new IllegalArgumentException("Invalid locator type: " + locatorType);
}
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsClickable(element);
String isChecked = element.getAttribute("checked");
boolean isCurrentlyChecked = isChecked != null && isChecked.equalsIgnoreCase("true");
if (turnOn && !isCurrentlyChecked) {
element.click();
} else if (!turnOn && isCurrentlyChecked) {
element.click();
}
} catch (NoSuchElementException e) {
throw new NoSuchElementException("Toggle Button Not Found with " + locatorType + ": " + locatorValue);
}
}

// Method to hide keyboard on screen
public static void closeKeyboard (AppiumDriver<MobileElement> driver, MobileElement textField) {
try {
WaitElementAction.getWaitsAppium(driver).waitUntilMobileElementIsVisible(textField);
textField.click();
driver.hideKeyboard();
} catch (NoSuchElementException e) {
throw new NoSuchElementException("TextField Not Found for closing keyboard");
}
}
}
Loading

0 comments on commit cf00335

Please sign in to comment.