In order to test the duplication in registering,I need to close and relaunch the iOS application.Can any one provide the best and simple scripts to close an application and relaunch the same in iOS using Appium and JAVA?
You can achieve the following way :
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class SampleTest {
AppiumDriver<?> appiumDriver;
final String URL_STRING = "http://127.0.0.1:4723/wd/hub";
URL url = new URL(URL_STRING);
public SampleTest() throws MalformedURLException {
}
#BeforeTest
public void beforeTest() {
File appDir = new File("path to ipa file");
File app = new File(appDir, "app.ipa");
DesiredCapabilities iOSCapabilities = new DesiredCapabilities();
iOSCapabilities.setCapability(MobileCapabilityType.PLATFORM, "iOS");
iOSCapabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "XCUITest");
iOSCapabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "iPhone6s");
iOSCapabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11.2.1");
iOSCapabilities.setCapability(MobileCapabilityType.UDID, "udid");
iOSCapabilities.setCapability(MobileCapabilityType.APP, app.getAbsolutePath());
appiumDriver = new IOSDriver(url, iOSCapabilities);
}
#Test
public void test() {
//perform some test
}
#AfterTest
public void afterTest() {
// following command will close and restart the app
appiumDriver.resetApp();
}
}
You may want to check this for more details.
use the driver driver.resetApp(), like this
try{
LOG.debug("reset app");
driver.resetApp();
} catch (WebDriverException e){
LOG.error("ERROR on reset app, e: ",e);
}
Appium allows you to start any pre-installed app on the device, so you can use closeApp first and then launchApp with your app bundle id:
// closing app
driver.closeApp();
// launch app again
HashMap<String, Object> args = new HashMap<>();
args.put("bundleId", APP_BUNDLE_ID);
driver.executeScript("mobile: launchApp", args);
Related
I am trying to run parallel test through selenium grid.
I know I have to use "thread local" for parallel execution,
but I have a problem with my code.
Cannot invoke "io.appium.java_client.android.AndroidDriver.findElementByAccessibilityId(String)" because "driver" is null
can you please solve it
package appiumset;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
public class _2_Deviceinfo {
public ThreadLocal<AppiumDriver> driver = new ThreadLocal<>();
public void setDriver(AppiumDriver driver) {
this.driver.set(driver);
}
public AppiumDriver getDriver() {
return this.driver.get();
}
#Parameters({"device", "apppackage", "activity","version","appiumServer" , "systemPort", "platformName"})
#BeforeMethod
public synchronized void deviceSetUp(String device, String apppackage, String activity, String version, String appiumServer, String systemPort, String platformName) throws InterruptedException, MalformedURLException {
System.out.println("****************************************");
System.out.println("Setting up device and desired capabilities");
DesiredCapabilities cap = new DesiredCapabilities();
URL url = new URL(appiumServer);
setDriver(new AndroidDriver<>(url, cap));
cap.setCapability(MobileCapabilityType.DEVICE_NAME, device);
cap.setCapability(MobileCapabilityType.UDID, device);
cap.setCapability(AndroidMobileCapabilityType.SYSTEM_PORT, systemPort);
cap.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 120);
cap.setCapability(MobileCapabilityType.PLATFORM_NAME, platformName);
//cap.setCapability(MobileCapabilityType., BrowserType.ANDROID);
cap.setCapability(MobileCapabilityType.PLATFORM_VERSION, version);
cap.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, apppackage);
cap.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, activity);
cap.setCapability("automationName", "UiAutomator2");
cap.setCapability("noReset","false");
cap.setCapability("FullReset","true");
cap.setCapability("APP_WAIT_ACTIVITY", "*");
cap.setCapability("autowebview","false");
}
#AfterMethod
public void closeDriver() {
getDriver().quit();
}
}
I can't find the driver (AppiumDriver)
package appiumset;
import java.net.MalformedURLException;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.Test;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.AndroidDriver;
public class _3_Onboarding extends _1_Appstart {
#Test
public void onboarding() throws MalformedURLException, InterruptedException {
System.out.println("_3_Onboarding Start");
MobileElement arrow = driver.findElementByAccessibilityId("next");
arrow.click();
System.out.println("next-done");
}
}
call getdriver() method of your class _2_Deviceinfo to your test class.
Appiumdriver<?> driver = getDriver();
MobileElement arrow = driver.findElementByAccessibilityId("next");
arrow.click();
I have a app in which i need to scan the QR code. Taking the picture from the app is not feasible as i need to run the app in multiple devices at once and it require human presence. How can i provide the QR code image/data to the app without scanning? Is there any way possible to simulate taking of picture and give store image as input to app?
If you have scanned test "QR Code image" then you can push it to the device from where app can read it.
You can ask dev team about the path from where app is reading the scanned image, and at same path you can push the test image.
Below is the code for how to push image file to device and other methods to push/pull different file formats
import java.awt.image.BufferedImage;
import io.appium.java_client.android.AndroidDriver;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import org.apache.commons.codec.binary.Base64;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
#Test
public class pushFileTest {
public static AndroidDriver<WebElement> _driver;
#BeforeClass
public void setUpAppium() throws InterruptedException, IOException {
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability("platformVersion","5.1");
cap.setCapability("platformName","Android");
cap.setCapability("deviceName","ZX12222D");
cap.setCapability("appPackage","io.appium.android.apis");
cap.setCapability("appActivity","ApiDemos");
//System.out.println("Before calling appium");
_driver = new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4725/wd/hub"), cap);
//System.out.println("After calling appium");
}
#Test
public void pullImageFileFromMobileSDCardTest() throws IOException {
byte[] returnData = _driver.pullFile("/storage/sdcard1/IMG_20140828_072840.jpg");
//System.out.println("Base 64 Converted String received from mobile :: " + returnData);
BufferedImage image=ImageIO.read(new ByteArrayInputStream(returnData));
ImageIO.write(image, "jpg", new File("C:\\eclipse","snap.jpg"));
}
/* Test Case to pull log file from mobile device*/
#Test
public void pullTextFileFromMobileSDCardTest() throws IOException {
byte[] returnData = _driver.pullFile("/storage/sdcard1/mili_log.txt");
//System.out.println(" Printing Text of File received from mobile :: " + new String(Base64.decodeBase64(returnData)));
File fs = new File("C:\\eclipse\\MobileFile.txt");
FileOutputStream fos = new FileOutputStream(fs);
fos.write(returnData);
fos.flush();
fos.close();
}
#Test
public void pushImageFileToMobileTest() throws IOException {
File fi = new File("C:\\eclipse\\img1.jpg");
byte[] fileContent = Files.readAllBytes(fi.toPath());
_driver.pushFile("/storage/sdcard1", fileContent);
}
#Test
public void pushTextFileToMobileTest() throws IOException {
File fi = new File("C:\\eclipse\\MobileFile.txt");
byte[] data = Files.readAllBytes(fi.toPath());
System.out.println("Base 64 Converted String sent to mobile :: " + data);
_driver.pushFile("/storage/sdcard1/appium.txt",data);
}
public void pullVideoFileFromMobileSDCardTest() throws IOException {
byte[] returnData = _driver.pullFile("/storage/sdcard1/VideoIconfile.mp4");
//System.out.println(" Printing Text of File received from mobile :: " + new String(Base64.decodeBase64(returnData)));
//File fs = new File("C:\\eclipse\\video.mp4");
FileOutputStream fos = new FileOutputStream("C:\\eclipse\\video.mp4");
fos.write(returnData);
fos.flush();
fos.close();
}
#AfterTest(alwaysRun= true)
public void tearDown(){
if (_driver!= null )
_driver.quit();
System.out.println("tearDown() :: driver.quit() executed");
}
}
I am trying to scroll down to an element, looked and searched everywhere and no code is helping to scroll down, i have tried with the code below which seems to be not working, anyone gives me the solution to scroll down perfectly.
As swipe and scrollTo functions are depreciated in the latest java client version, a perfect code will help me to solve my TASK
package mobileapp.com.example;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.pagefactory.AndroidFindBy;
public class Day5 {
AndroidDriver<WebElement> driver;
#AndroidFindBy (uiAutomator = "new UiScrollable(new UiSelector()).scrollIntoView(new UiSelector().textContains(\"PHP\"))")
public WebElement scrollStepOne;
#BeforeTest
public void setup() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("BROWSER_NAME", "Android");
capabilities.setCapability("VERSION", "6.0.1");
capabilities.setCapability("deviceName","Nexus 5");
capabilities.setCapability("platformName","Android");
capabilities.setCapability("appPackage", "com.vector.guru99");
capabilities.setCapability("appActivity","com.vector.guru99.BaseActivity");
driver= new AndroidDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
#Test
public void StartTest() throws InterruptedException {
//Verify Homepage
if(driver.findElement(By.id("android:id/action_bar_title")).isDisplayed())
System.out.println("Home page is displayed");
else
System.out.println("Home page is not displayed");
//step2 - click on Course List tab
driver.findElement(By.name("Course List")).click();
System.out.println("Courses list are : ");
Thread.sleep(3000);
//Step 3 - darg until PHP course found and click on it
scrollStepOne.click();
Thread.sleep(3000);
// driver.findElement(By.xpath("//android.widget.TextView[#text()='PHP']")).click();
// Thread.sleep(3333);
//Step 4 - Click on lesson 1 and verify
driver.findElement(By.xpath("//android.widget.TextView[#text='What is PHP? Write your first PHP Program']")).click();
Thread.sleep(3333);
if(driver.findElement(By.id("com.vector.guru99:id/lesson_title")).isDisplayed())
System.out.println("First Lesson is displayed");
else
System.out.println("First lesson not opened");
}
#AfterTest
public void tearDown() {
driver.quit();
}
}
for scrolling to a element have a text as App_Settings, you can use the below
androidDriver.findElementByAndroidUIAutomator("new UiScrollable(new UiSelector().scrollable(true).instance(0))" +
".scrollIntoView(new UiSelector().description(\"App_Settings\"));");
I have the following setup and trying to start the test on iOS Simulator. I am a beginner and don't know how to start the test. I have already imported and installed appium from tutorials.
The questions are:
Is this setup correct?
How to run the tests?
import java.io.File;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
public class AppiumDriver {
private static final String JavascriptExecutor = null;
public WebDriver driver = null;
#BeforeMethod
public void setUp() throws Exception {
// set up appium
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "iOS");
capabilities.setCapability(CapabilityType.VERSION, "8.1");
capabilities.setCapability(CapabilityType.PLATFORM, "Mac");
capabilities.setCapability("device", "iPhone");
capabilities.setCapability("app", "path here, i have started it with appium inspector and it works");
driver = new RemoteWebDriver(new URL("http://127.0.0.1:4725/wd/hub"), capabilities);
System.out.println("App launched");
}
#Test
public void test01() throws InterruptedException {
driver.findElement(By.name("Guest")).click();
Thread.sleep(5000);
}
#AfterMethod
public void tearDown() throws Exception {
driver.quit();
}
}
Yes, your setup is correct. Looks like you are using testng framework so just run it as "testng" and you should be good to go.
I tried displaying the google web page on my javafx view using webview. All it does is display an empty page. For testing I did add a text element at the bottom and it did show up. Any pointers would be helpful. My code and the sample screen are attached.
I am running this application on a Windows 7 machine with 8 GB RAM and this is deployed in an environment that needs proxy authentication.
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
public class MyBrowser extends Application
{
private Pane root;
#Override
public void start(final Stage stage) throws URISyntaxException
{
root = new VBox();
List<Proxy> proxies = ProxySelector.getDefault().select(new URI("http://www.google.com"));
final Proxy proxy = proxies.get(0); // ignoring multiple proxies to simplify code snippet
if (proxy.type() != Proxy.Type.DIRECT)
{
// you can change that to dialog using separate Stage
final TextField login = new TextField("login");
final PasswordField pwd = new PasswordField();
Button btn = new Button("Login");
btn.setOnAction(new EventHandler<ActionEvent>()
{
public void handle(ActionEvent t)
{
System.setProperty("http.proxyUser", login.getText());
System.setProperty("http.proxyPassword", pwd.getText());
displayWebView();
}
});
root.getChildren().addAll(login, pwd, btn);
}
else
{
displayWebView();
}
stage.setScene(new Scene(root, 400, 600));
stage.show();
}
private void displayWebView()
{
root.getChildren().clear();
WebView webView = new WebView();
final WebEngine webEngine = webView.getEngine();
root.getChildren().addAll(webView, new Text("HELLO"));
webEngine.load("http://www.google.com");
}
public static void main(String[] args)
{
launch();
}
}
I copied and pasted your code and ran it on Windows 7 with Java7u40 both Java8b108.
In both cases the code functioned correctly and displayed the http://www.google.com page.
The proxy selector code in your source was not triggered for me (probably because I have a Proxy.Type.DIRECT connection, so there was nothing for it to do).