Balaji Santhanam
Balaji Santhanam

Reputation: 31

unable to find a way to run my code in sauce labs real time device

How to run the below Appium code in sauce labs? When I checked sauce labs website there is only one line given below

driver = new WebDriver(
    new URL("https://balajimscit09:a30f3417-cbe6-48ce-92b5-e9a6d0814879@ondemand.us-west-1.saucelabs.com:443")
);

Below is my code

package mobile_Appium;
import static io.appium.java_client.touch.TapOptions.tapOptions;
import static io.appium.java_client.touch.WaitOptions.waitOptions;
import static io.appium.java_client.touch.offset.ElementOption.element;
 
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
 
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
 
 
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.FindsByAndroidUIAutomator;
import io.appium.java_client.MobileElement;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
 
public class InstallTestAndroid10 {
 
    static AppiumDriver driver;
    

    public static void main(String[] args) throws MalformedURLException, InterruptedException {
        File f = new File("src");
        File fs = new File(f, "ApiDemos-debug.apk");
        DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        cap.setCapability(MobileCapabilityType.VERSION, "10.0");
        cap.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Device");
        cap.setCapability(MobileCapabilityType.AUTOMATION_NAME, "Uiautomator2");
        
        cap.setCapability("autoGrantPermissions", true);
        cap.setCapability("noReset", "false");
        cap.setCapability("fullReset", "true");
        
        cap.setCapability(MobileCapabilityType.APP, fs.getAbsolutePath());
        driver = new AndroidDriver<>(new URL("http://127.0.0.1:4723/wd/hub"), cap);
        driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
        
        /*driver.findElement(By.xpath("//android.widget.Button[@text='OK']")).click();
        Thread.sleep(10000);
        
         ((FindsByAndroidUIAutomator<MobileElement>) driver).findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().textContains(\"Views\").instance(0))");
        driver.findElement(By.xpath("//android.widget.TextView[@text='Views']")).click();   */
}}

How to integrate with a real device present in sauce labs?

Upvotes: 0

Views: 463

Answers (2)

walkerlj0
walkerlj0

Reputation: 101

In those capabilities, it looks like you are still pointing to a local URL. You need to add a URL for sauce labs with your username and access key, and upload an app. See how it is done in this video: https://www.youtube.com/watch?v=hwp5YeF5Me4

There are 3 basic things that you need to do to run an Appium test

  1. Upload your app to Sauce Labs so your test can run against it in the Real Device Cloud
  2. Update your Test code with your Sauce Username and Access Key (Set as environment vars), and use these to start a driver with the endpoint (or URL) to test against
  3. Update your capabilities for the real device you want to test including app name, device, platform version and more.
        System.out.println("Sauce iOS Native - BeforeMethod hook");
        String username = System.getenv("SAUCE_USERNAME");
        String accesskey = System.getenv("SAUCE_ACCESS_KEY");
        String sauceUrl;
        if (region.equalsIgnoreCase("eu")) {
            sauceUrl = "@ondemand.eu-central-1.saucelabs.com:443";
        } else {
            sauceUrl = "@ondemand.us-west-1.saucelabs.com:443";
        }
        
        String SAUCE_REMOTE_URL = "https://" + username + ":" + accesskey + sauceUrl +"/wd/hub";
        String appName = "iOS.RealDevice.SauceLabs.Mobile.Sample.app.2.7.1.ipa";
        String methodName = method.getName();
        URL url = new URL(SAUCE_REMOTE_URL);

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("deviceName", "iPhone 8.*");
        capabilities.setCapability("platformName", "iOS");
        capabilities.setCapability("automationName", "XCuiTest");
        capabilities.setCapability("app", "storage:filename="+appName); // or "storage:"+appID
        capabilities.setCapability("name", methodName);
        iosDriver.set(new IOSDriver(url, capabilities));

Upvotes: 1

Eyal Y
Eyal Y

Reputation: 31

Your App should be upload to sauce storage. After that, the app capability should point to this file. Fo example: cap.setCapability(MobileCapabilityType.APP, "storage:filename=ApiDemos-debug.apk");

You can read more here: https://wiki.saucelabs.com/display/DOCS/Application+Storage

Also, you should change your access key after publishing it here

Upvotes: 1

Related Questions