Created
September 12, 2019 08:31
-
-
Save KrishnB/28c9190da9352467702143f8f10bbf06 to your computer and use it in GitHub Desktop.
Otp Reader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package utils; | |
import com.testvagrant.commons.entities.SmartBOT; | |
import io.appium.java_client.AppiumDriver; | |
import io.appium.java_client.android.Activity; | |
import io.appium.java_client.android.AndroidDriver; | |
import io.appium.java_client.android.appmanagement.AndroidInstallApplicationOptions; | |
import org.apache.commons.lang3.StringUtils; | |
import org.openqa.selenium.By; | |
import org.openqa.selenium.WebElement; | |
import org.openqa.selenium.support.FindBy; | |
import org.openqa.selenium.support.PageFactory; | |
import pages.BasePage; | |
import java.time.Duration; | |
import java.util.List; | |
import java.util.Map; | |
import java.util.Optional; | |
public class OtpReader { | |
private SmartBOT smartBOT; | |
private String APP_PATH = "https://github.com/testvagrant/optimusTemplate/releases/download/v1/otp-reader.apk"; | |
private String APP_ACTIVITY = "otpreader.testvagrant.com.otpreader.OTPViewActivity"; | |
private String APP_PACKAGE = "otpreader.testvagrant.com.otpreader"; | |
private String otpText = ""; | |
public OtpReader(SmartBOT smartBOT) { | |
this.smartBOT = smartBOT; | |
} | |
public String getOtp() { | |
activateApp(APP_PACKAGE); | |
OtpPage otpPage = new OtpPage(smartBOT.getDriver()); | |
String otp = otpPage.getOtpText(); | |
System.out.println("Otp is "+otp); | |
activateApp(smartBOT.getAppPackageName()); | |
return otp; | |
} | |
public void setupOtpReaderApk() { | |
installApp(); | |
try { | |
activateApp(APP_PACKAGE); | |
} catch (Exception e) { | |
} | |
activateApp(smartBOT.getAppPackageName()); | |
} | |
public void tearOtpReaderApk() { | |
smartBOT.getDriver().terminateApp(APP_PACKAGE); | |
} | |
private boolean isAppInstalled() { | |
return smartBOT.getDriver().isAppInstalled(APP_PACKAGE); | |
} | |
private void installApp() { | |
AndroidInstallApplicationOptions androidInstallApplicationOptions = new AndroidInstallApplicationOptions().withGrantPermissionsEnabled(); | |
if(!isAppInstalled()) { | |
smartBOT.getDriver().installApp(APP_PATH, androidInstallApplicationOptions); | |
} | |
} | |
private void activateApp(String packageName) { | |
smartBOT.getDriver().activateApp(packageName); | |
} | |
private class OtpPage extends BasePage { | |
@FindBy(id = "otp") | |
private WebElement otpElement; | |
private AppiumDriver appiumDriver; | |
public OtpPage(AppiumDriver appiumDriver) { | |
super(appiumDriver); | |
this.appiumDriver = appiumDriver; | |
PageFactory.initElements(appiumDriver, this); | |
} | |
public String getOtpText() { | |
By otpBy = By.xpath("//*"); | |
waitForPresenceOfAllElements(otpBy); | |
List<WebElement> elements = ((AndroidDriver)appiumDriver).findElements(otpBy); | |
Optional<WebElement> first = elements.stream().filter(element -> element.getText().startsWith("otp:")).findFirst(); | |
if(first.isPresent()) { | |
otpText = first.get().getText(); | |
System.out.println("Otp text "+StringUtils.getDigits(otpText)); | |
return StringUtils.getDigits(otpText); | |
} else { | |
getOtpText(); | |
} | |
return StringUtils.getDigits(otpText); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment