A selenium-webdriver extension to support native app testing by Mobile JSON Wire Protocol which appium supports, also provides easy wait for element for pages or navigation among pages, and supports PageObject pattern
npm install selenium-appiumhttp://localhost:4723/wd/hub to http://localhost:4723. The goal is to remove appium from the project and use winappdriver to launch and stop WinAppDriver
npm i selenium-appium --save-dev
`
$3
- By2 is a subclass of selenium-webdriver.By. So you can use it anywhere which selenium-webdriver provides.
- Also By2 implements the WebElement interface, so you can call click and other element operation directly.
- No webdriver.wait needed. Instead of findElement, By2 itself loop until element is located before actual click happens.
`
test("By2 used in selenium-webdriver.WebDriver", async () => {
const webdriver = await new Builder()
.usingServer(url)
.withCapabilities(capabilities)
.build();
const element = await webdriver.wait(until.elementLocated(By2.nativeName('One')));
await element.click();
await webdriver.quit();
});
test("By2 Supports multiple webdriver2", async () => {
const webdriver = new WebDriver2();
await webdriver.startWithCapabilities(capabilities)
await By2.nativeName('One', webdriver).click();
await webdriver.quit();
});
test("By2 deduced WebDriver2 for single WebDriver2", async () => {
await driver.startWithCapabilities(capabilities)
await By2.nativeName('One').click();
await driver.quit();
});
`
$3
- driver is a wrapper of WebDriver which selenium-webdriver provides.
- Implements all functions of WebDriver of selenium-webdriver.
- It provides the default driver for By2.
- It delayed the creation of WebDriver, and implement the interface of WebDriver.
There are two ways to initialize the driver: startWithWebDriver or startWithWebDriver.
startWithWebDriver allows you to attach the driver to existing WebDriver if capability is not enough for your testing.
`
test("simple webdriver2, and driver create from WebDriver", async () => {
const webdriver = await new Builder()
.usingServer(url)
.withCapabilities(capabilities)
.build();
await driver.startWithWebDriver(webdriver);
await By2.nativeName('One').click();
await webdriver.quit();
});
test("Multiple webdriver2", async () => {
const webdriver = new WebDriver2();
await webdriver.startWithCapabilities(capabilities)
await By2.nativeName('One', webdriver).click();
await webdriver.quit();
});
test("Simple Webdriver2, and Driver create from capabilities", async () => {
await driver.startWithCapabilities(capabilities)
await By2.nativeName('One').click();
await driver.quit();
});
`
$3
PageObject reduces the amount of duplicated code and easy to maintain.
1. get in typescript would make you always get new instance of By2. For example, get resultTextBox()
2. waitForPageLoaded pairs with isPageLoaded. You only need to overload isPageLoaded. waitForPageLoaded would poll until isPageLoaded is true or timeout.
3. In practice, we only need simple instance of PageObject since it doesn't save state, and all state could be query by By2 from remote app.
PageObject
`
import { PageObject, By2 } from "selenium-appium";
class CalculatorPage extends PageObject {
isPageLoaded() {
return this.minusButton.isDisplayed();
}
get resultTextBox() { return By2.nativeAccessibilityId('CalculatorResults');}
get equalButton() { return By2.nativeAccessibilityId('equalButton'); }
get clearButton() { return By2.nativeName('Clear'); }
get plusButton() { return By2.nativeName('Plus'); }
get divideButton() { return By2.nativeAccessibilityId('divideButton'); }
get multiplyButton() { return By2.nativeXpath("//Button[@Name='Multiply by']") }
get minusButton() { return By2.nativeXpath("//Button[@AutomationId=\"minusButton\"]"); }
private async pressKeys(keys: string) {
for (var key of keys) {
await By2.nativeAccessibilityId('num' + key + 'Button').click();
}
}
async divid(a: string, b: string): Promise {
await this.pressKeys(a);
await this.divideButton.click();
await this.pressKeys(b);
await this.equalButton.click();
return await this.getCalculatorResultText();
}
async multiply(a: string, b: string): Promise {
await this.pressKeys(a);
await this.multiplyButton.click();
await this.pressKeys(b);
await this.equalButton.click();
return await this.getCalculatorResultText();
}
async plus(a: string, b: string): Promise {
await this.pressKeys(a);
await this.plusButton.click();
await this.pressKeys(b);
await this.equalButton.click();
return await this.getCalculatorResultText();
}
async minus(a: string, b: string): Promise {
await this.pressKeys(a);
await this.minusButton.click();
await this.pressKeys(b);
await this.equalButton.click();
return await this.getCalculatorResultText();
}
private async getCalculatorResultText(): Promise {
return (await this.resultTextBox.getText()).replace('Display is', '').trim();
}
}
export default new CalculatorPage();
`
use the PageObject
`
beforeEach(async () => {
await CalculatorPage.waitForPageLoaded();
await CalculatorPage.clearButton.clear();
});
test('Addition', async () => {
// Find the buttons by their names and click them in sequence to perform 1 + 7 = 8
expect(await CalculatorPage.plus('1', '7')).toBe('8');
});
test('Division', async () => {
// Find the buttons by their accessibility ids and click them in sequence to perform 88 / 11 = 8
expect(await CalculatorPage.divid('88', '11')).toBe('8');
});
test('Multiplication', async () => {
// Find the buttons by their names using XPath and click them in sequence to perform 9 x 9 = 81
expect(await CalculatorPage.multiply('9', '9')).toBe('81');
});
test('Subtraction', async () => {
// Find the buttons by their accessibility ids using XPath and click them in sequence to perform 9 - 1 = 8
expect(await CalculatorPage.minus('9', '1')).toBe('8');
});
`
$3
There are two global timers: waitforPageTimeout and waitforTimeout.
waitforTimeout is used by By2. When call any WebElement function of By2, it loops until the element is located
waitforPageTimeout is used by PageObject. It defined the default timeout for waitForPageLoaded.
`
Config.setWaitForPageTimeout(100);
expect(Config.getWaitForPageTimeout()).toBe(100);
``