Appium和iOS Mobile Safari自动化,能做到吗?

Posted

技术标签:

【中文标题】Appium和iOS Mobile Safari自动化,能做到吗?【英文标题】:Appium and iOS Mobile Safari automation, can it be done? 【发布时间】:2013-05-16 16:54:03 【问题描述】:

我无法在 ios 模拟器上使用 Appium 与 Safari 移动浏览器进行任何自动化操作。在我的 Java 项目中,Safari 将启动,但浏览器甚至不会导航到指定的网站。谁能告诉我我的代码做错了什么?

1) 在我的 OSX 机器上启动 Appium 应用程序。它配置了以下设置:

IP Address: 127.0.0.1
Port: 4723
Force Device: Checked - iPhone
User Mobile Safari: Checked

(注意:当我运行我的项目时,Appium 应用程序日志屏幕上没有消息滚动。以前,我收到有关缺少 iOS 6.0 库的投诉,但是当我复制 6.1 iOS 库然后将目录重命名为6.0,消息消失了。

2) 启动 Eclipse 并打开 Appium 项目

3) 右键单击​​测试代码,然后单击 RunAs Junit

4) iPhone 模拟器启动 -- iPhone iOS 6.1

5) Mobile Safari 启动...然后无处可去(应该去 cnn.com)。我没有收到任何错误。

Appium Java 项目实际上可以用于移动 Safari 自动化吗?我在 Appium 示例代码库中没有看到任何 Safari 自动化示例。

什么给了?

谢谢,

拉里

------------------下面的Java代码-------------------------- --------------

Eclipse Juno 正在用于运行我的 Java/Appium 项目。以下是 Java JUnit 项目代码的简化列表(当相应修改并与 iWebDriver 和已弃用的 iPhoneDriver() 一起使用时,可以正常工作):

import org.junit.Before;
import org.junit.After;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

public class AppiumiPhoneWebDriverDemo 

    private String baseUrl;
    private WebDriver driver;

    @Before
    public void setup() throws Exception
    

        WebDriver driver;
        DesiredCapabilities cap = new DesiredCapabilities();
        //cap.setCapability("", "");
        //cap.setCapability("browsername", "");
        //cap.setCapability("os", "iOS 6.1");
        cap.setCapability("device", "iPhone Simulator");
        cap.setCapability("app", "safari");

        driver = new RemoteWebDriver(new URL("http://localhost:4723/wd/hub"), cap);

    baseUrl = "http://www.cnn.com";

       

    @After
    public void tearDown() throws Exception
    

    driver.quit();

    


    @Test
    public void test_searchWorks() throws Exception
    
        this.driver.get(baseUrl);

        driver.quit();
    

  

【问题讨论】:

【参考方案1】:

你绝对可以做到。

查看此代码

"use strict";

require("./helpers/setup");

var wd = require("wd"),
    _ = require('underscore'),
    serverConfigs = require('./helpers/appium-servers');

describe("ios safari", function () 
  this.timeout(300000);
  var driver;
  var allPassed = true;

  before(function () 
    var serverConfig = process.env.SAUCE ?
      serverConfigs.sauce : serverConfigs.local;
    driver = wd.promiseChainRemote(serverConfig);
    require("./helpers/logging").configure(driver);

    var desired = _.clone(require("./helpers/caps").ios81);
    desired.browserName = 'safari';
    if (process.env.SAUCE) 
      desired.name = 'ios - safari';
      desired.tags = ['sample'];
    
    return driver.init(desired);
  );

  after(function () 
    return driver
      .quit()
      .finally(function () 
        if (process.env.SAUCE) 
          return driver.sauceJobStatus(allPassed);
        
      );
  );

  afterEach(function () 
    allPassed = allPassed && this.currentTest.state === 'passed';
  );


  it("should get the url", function () 
    return driver
      .get('https://www.google.com')
      .sleep(1000)
      .waitForElementByName('q', 5000)
        .sendKeys('sauce labs')
        .sendKeys(wd.SPECIAL_KEYS.Return)
      .sleep(1000)
      .title().should.eventually.include('sauce labs');
  );

  it("should delete cookie passing domain and path", function () 
    var complexCookieDelete = function(name, path, domain) 
      return function() 
        path = path || '|';
        return driver.setCookie(name: name, value: '', path: path, 
          domain: domain, expiry: 0);        
      ;
    ;

    return driver
      .get('http://en.wikipedia.org')
      .waitForElementByCss('.mediawiki', 5000)
      .allCookies() // 'GeoIP' cookie is there
      .deleteCookie('GeoIP') 
      .allCookies() // 'GeoIP' is still there, because it is set on
                    // the .wikipedia.org domain
      .then(complexCookieDelete('GeoIP', '/', '.wikipedia.org'))
      .allCookies() // now 'GeoIP' cookie is gone
      .sleep(1000);
  );

);

【讨论】:

【参考方案2】:

为什么要在 setUp 方法中定义第二个 WebDriver ?删除第二个定义以设置类成员驱动程序。

【讨论】:

【参考方案3】:
  DesiredCapabilities cap = new DesiredCapabilities();
        //cap.setCapability("", "");
        //cap.setCapability("browsername", "");
        //cap.setCapability("os", "iOS 6.1");
        cap.setCapability("device", "iPhone Simulator");
        cap.setCapability("app", "safari");

应该是

  DesiredCapabilities cap = new DesiredCapabilities();
        cap.setCapability("deviceName", "iPhone Simulator");
        cap.setCapability("browsername", "safari");
        cap.setCapability("platformVersion", "7.1");
        cap.setCapability("platformName", "iOS");

它对我有用。希望它能解决您的问题。祝你好运。

【讨论】:

【参考方案4】:

假设您在 Mac 上使用 Java,这对我有用,包括启动 Appium 服务本身的代码,然后是连接到它的驱动程序:

package baseTest;

import com.groupon.tests.mainapp.pages.*;
import io.appium.java_client.ios.IOSDriver;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import io.appium.java_client.service.local.AppiumServiceBuilder;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;

import java.io.File;
import java.io.IOException;
import java.net.URL;


public class AppiumSafariBaseUITest 

private WebDriver wd;

protected WebDriver getDriver()
    return wd;


String nodePath = "/Applications/Appium.app/Contents/Resources/node/bin/node";
String appiumJSPath = "/usr/local/lib/node_modules/appium/build/lib/main.js";

AppiumDriverLocalService service;
String service_url;

private void startAppiumServer() throws IOException 
    service = AppiumDriverLocalService.buildService(new AppiumServiceBuilder()
                    .usingPort(4890)
                    .usingDriverExecutable(new File(nodePath))
                    .withAppiumJS(new File(appiumJSPath))
    );
    service.start();


@BeforeClass(alwaysRun = true)
public void setUpAppiumDriver() throws IOException 

    startAppiumServer();

    service_url = service.getUrl().toString();

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability("appium-version", "1.0");
    capabilities.setCapability("platformName", "iOS");
    capabilities.setCapability("platformVersion", "9.3");
    capabilities.setCapability("deviceName", "iPhone 5s");
    capabilities.setCapability("newCommandTimeout", 600);
    capabilities.setCapability("bundleId", "com.apple.mobilesafari");
    capabilities.setCapability("useLocationServices", false);

    wd = new IOSDriver(new URL(service_url), capabilities);


@BeforeMethod(alwaysRun = true)
public void beforeMethod()
    if(!service.isRunning())
    
        service.start();
    


@AfterClass(alwaysRun = true)
public void killSimulatorAndAppium()
    service.stop();


【讨论】:

以上是关于Appium和iOS Mobile Safari自动化,能做到吗?的主要内容,如果未能解决你的问题,请参考以下文章

iOS 5.1 和 Safari Mobile 限制

Appium+python自动化16-启动ios上Safari浏览器

div背景图像和iOS Mobile Safari兼容性问题

iOS 7 Safari 干扰 jQuery Mobile 页脚

在 iOS Mobile Safari 和/或 UIWebView 中控制文本选择行为?

iOS Mobile Safari 纵向布局过于缩放