Skip to content

Instantly share code, notes, and snippets.

@maksimlaptev
Created June 19, 2023 11:56
Show Gist options
  • Save maksimlaptev/ece6d82692f9f7e1d5e1751493c690ae to your computer and use it in GitHub Desktop.
Save maksimlaptev/ece6d82692f9f7e1d5e1751493c690ae to your computer and use it in GitHub Desktop.
how to configure the main class to work with appium
import com.codeborne.selenide.WebDriverRunner;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import io.qameta.allure.selenide.AllureSelenide;
import org.aeonbits.owner.ConfigFactory;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import static com.codeborne.selenide.logevents.SelenideLogger.addListener;
public class Env_mobile{
public AppiumDriver driver;
File app;
AppiumDriverLocalService service;
@BeforeSuite
//Passing parameters from a file testng.xml
// @Parameters({"platform"})
public void setup() throws MalformedURLException {
app = new File("PathToApk");
// get the configuration for the driver
getConfiguration("android");
// driver installation for selenide
WebDriverRunner.setWebDriver(driver);
// Required to get a screenshot after each unsuccessful test
addListener("AllureSelenide", new AllureSelenide().screenshots(true).savePageSource(true));
}
/** getting the configuration for the desired platform
*/
public void getConfiguration(String runType) throws MalformedURLException {
switch (runType){
case("android"):
driver = getAndroidDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));
break;
}
}
/** define common capabilities
*/
private DesiredCapabilities commonCapabilities() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("project", "autotests.cloud");
capabilities.setCapability("autoGrantPermissions", true);
return capabilities;
}
/**
Getting configuration for android
deviceName - device name (emulator or physical device). Can be obtained using the command - adb devices
app - application - path to the .apk file
appPackage and appActivity - Can be obtained from developers or via command adb shell
*/
public AppiumDriver getAndroidDriver() throws MalformedURLException {
DesiredCapabilities capabilities = commonCapabilities();
capabilities.setCapability("deviceName", data.getDeviceName());
capabilities.setCapability("platformName", "android");
capabilities.setCapability("app", app.getAbsolutePath());
capabilities.setCapability("appPackage", "com.google.android.calculator");
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
// default path to android server http://127.0.0.1:4723/wd/hub"
return new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
/** method for starting appium server automatically **/
public void upAppium(){
AppiumServiceBuilder appiumServiceBuilder = new AppiumServiceBuilder();
appiumServiceBuilder
.usingAnyFreePort()
.withArgument(() -> "--allow-insecure", "chromedriver_autodownload");
appiumServiceBuilder.withAppiumJS(new File("/home/hany/appium/server/app/main.js"));
service = AppiumDriverLocalService.buildService(appiumServiceBuilder);
service.start();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment