Last active
August 9, 2019 19:24
-
-
Save ckzgraphics/1798f10c3a8469df2ed5e1cf1fa31ae2 to your computer and use it in GitHub Desktop.
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
import java.net.URL; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Set; | |
import java.io.File; | |
import java.net.MalformedURLException; | |
import io.appium.java_client.AppiumDriver; | |
import io.appium.java_client.MobileBy; | |
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.AndroidKeyCode; | |
import io.appium.java_client.android.AndroidKeyMetastate; | |
import org.openqa.selenium.OutputType; | |
import org.openqa.selenium.TakesScreenshot; | |
import org.openqa.selenium.WebDriver; | |
import org.openqa.selenium.remote.DesiredCapabilities; | |
/** | |
* APPIUM DOC - http://appium.io/docs/en/commands/device/keys/press-keycode/ | |
*/ | |
public class Camera_Access_Android { | |
public static String userName = "<BROWSERSTACK_USERNAME>"; | |
public static String accessKey = "<BROWSERSTACK_ACCESS_KEY>"; | |
public static String HUB_URL = "http://"+userName+":"+accessKey+"@hub-cloud.browserstack.com/wd/hub"; | |
public static void main(String args[]) throws MalformedURLException, InterruptedException { | |
DesiredCapabilities caps = null; | |
try{ | |
caps = new DesiredCapabilities(); | |
caps.setCapability("project", "Test Run"); | |
caps.setCapability("build", "Build v1"); | |
caps.setCapability("device", "Google Pixel 2"); | |
caps.setCapability("os_version", "9.0"); | |
caps.setCapability("app", "CustomMediaFile_Android"); | |
caps.setCapability("browserstack.debug", "true"); | |
// SELECT A FILE FROM GALLARY | |
Camera_Access_Android.select_image_from_gallery(caps); | |
} catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} // FUNC main END | |
public static void select_image_from_gallery(DesiredCapabilities caps){ | |
AndroidDriver driver = null; | |
MobileElement ele = null; | |
TouchAction touch_action = null; | |
try { | |
caps.setCapability("name", "Test: Capture Photo"); | |
driver = new AndroidDriver(new URL(HUB_URL), caps); | |
touch_action = new TouchAction(driver); | |
ele = (MobileElement) driver.findElement(MobileBy.xpath(".//android.widget.Button[@text='Camera']")); | |
ele.click(); | |
Thread.sleep(3000); | |
take_screenshot(driver); | |
driver.pressKeyCode(AndroidKeyCode.KEYCODE_CAMERA, AndroidKeyMetastate.META_SHIFT_ON); | |
Thread.sleep(3000); | |
take_screenshot(driver); | |
System.out.println(driver.getPageSource()); | |
ele = (MobileElement) driver.findElement(MobileBy.xpath(".//android.widget.Button[@text='Allow']")); | |
ele.click(); | |
Thread.sleep(2000); | |
take_screenshot(driver); | |
driver.pressKeyCode(AndroidKeyCode.KEYCODE_CAMERA, AndroidKeyMetastate.META_SHIFT_ON); | |
Thread.sleep(3000); | |
take_screenshot(driver); | |
System.out.println(driver.getPageSource()); | |
driver.pressKeyCode(AndroidKeyCode.KEYCODE_CAMERA, AndroidKeyMetastate.META_SHIFT_ON); | |
Thread.sleep(3000); | |
take_screenshot(driver); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} finally{ | |
if(driver != null){ | |
driver.quit(); | |
} | |
} | |
} // FUNC select_image_from_gallery END | |
public static void take_screenshot(WebDriver webDriver){ | |
try { | |
TakesScreenshot scrShot =((TakesScreenshot)webDriver); | |
File SrcFile=scrShot.getScreenshotAs(OutputType.FILE); | |
// File DestFile=new File("/Users/test/Documents/test.png"); | |
// FileUtils.copyFile(SrcFile, DestFile); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} // FUNC take_screenshot END | |
} // CLASS END |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment