我需要关闭应用程序并在我关闭它的同一位置重新启动它,因为这是使用 closeApp 和 launchApp 方法。但是当我启动应用程序时,它会从头开始启动应用程序。我尝试使用 noReset 和 fullReset 选项。我在 iPhone 7 模拟器上使用 appium 1.2.7 和 ios 11.2。在 android 中我能够使用 noReset 来实现这一点,这在 ios 中是否可行。
有人可以建议吗?
提前致谢。
我需要关闭应用程序并在我关闭它的同一位置重新启动它,因为这是使用 closeApp 和 launchApp 方法。但是当我启动应用程序时,它会从头开始启动应用程序。我尝试使用 noReset 和 fullReset 选项。我在 iPhone 7 模拟器上使用 appium 1.2.7 和 ios 11.2。在 android 中我能够使用 noReset 来实现这一点,这在 ios 中是否可行。
有人可以建议吗?
提前致谢。
做你想做的事情的唯一方法是保存你的 session_id,当你启动一个新测试时,你从之前执行的测试的终点开始......
你要做的是重写 Appium 构造函数和 start_session:
Python类代码:
class MyWebDriverAppium(webdriver.Remote):
def __init__(self, context, command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=None, session_id=None):
self.cCore = context
try:
self.preserved_session_id = session_id
self.error_handler = MobileErrorHandler()
self._switch_to = MobileSwitchTo(self)
# add new method to the `find_by_*` pantheon
By.IOS_UIAUTOMATION = MobileBy.IOS_UIAUTOMATION
By.IOS_PREDICATE = MobileBy.IOS_PREDICATE
By.ANDROID_UIAUTOMATOR = MobileBy.ANDROID_UIAUTOMATOR
By.ACCESSIBILITY_ID = MobileBy.ACCESSIBILITY_ID
super(MyWebDriverAppium, self).__init__(command_executor, desired_capabilities)
except Exception:
print(traceback.format_exc())
raise MyWebDriverAppiumInit("Error initializing Appium driver")
# If preserved_session is None we will start a new session as normal
# If preserved session is not None we will use that session to execute
def start_session(self, desired_capabilities, browser_profile=None):
try:
if self.preserved_session_id:
self.command_executor._commands['getSession'] = ('GET', '/session/$sessionId')
self.session_id = self.preserved_session_id
response = self.execute('getSession', {'sessionId ': self.session_id})
self.session_id = response['sessionId']
self.capabilities = response['value']
self.w3c = response['status']
else:
super(MyWebDriverAppium, self).start_session(desired_capabilities, browser_profile)
except Exception:
raise MyWebDriverAppiumStart("Error starting Appium driver after initialization")
Python实现代码:
self.driver = MyWebDriverAppium(self, self.driver_context, self.desired_caps, session_id)
Java类代码:
public class MyWebDriverAppium extends RemoteWebDriver {
private String preserved_session_id = null;
public MyWebDriverAppium(URL remoteAddress, Capabilities desiredCapabilities, String session_id) {
super(remoteAddress, desiredCapabilities);
this.preserved_session_id = session_id;
}
@Override
protected void startSession(Capabilities desiredCapabilities) {
if(this.preserved_session_id != null){
setSessionId(this.preserved_session_id);
}
super.startSession(desiredCapabilities);
}
}
Java实现代码:
driver = new MyWebDriverAppium<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities, session_id);
我知道 python 代码可以完美运行,因为它是我每次在测试中使用的代码。我不能 100% 确定 Java 代码会完美运行,因为我在没有 IDE 的情况下编写了它。
希望能帮助到你