Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Additional changes of the #473 #786

Merged
merged 5 commits into from
Dec 18, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/main/java/io/appium/java_client/MobileCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public class MobileCommand {
protected static final String IS_KEYBOARD_SHOWN;
protected static final String IS_LOCKED;
protected static final String LONG_PRESS_KEY_CODE;
protected static final String FINGER_PRINT;
protected static final String OPEN_NOTIFICATIONS;
protected static final String PRESS_KEY_CODE;
protected static final String PUSH_FILE;
Expand Down Expand Up @@ -116,6 +117,7 @@ public class MobileCommand {
IS_KEYBOARD_SHOWN = "isKeyboardShown";
IS_LOCKED = "isLocked";
LONG_PRESS_KEY_CODE = "longPressKeyCode";
FINGER_PRINT = "fingerPrint";
OPEN_NOTIFICATIONS = "openNotifications";
PRESS_KEY_CODE = "pressKeyCode";
PUSH_FILE = "pushFile";
Expand Down Expand Up @@ -170,6 +172,7 @@ public class MobileCommand {
commandRepository.put(IS_LOCKED, postC("/session/:sessionId/appium/device/is_locked"));
commandRepository.put(LONG_PRESS_KEY_CODE,
postC("/session/:sessionId/appium/device/long_press_keycode"));
commandRepository.put(FINGER_PRINT, postC("/session/:sessionId/appium/device/finger_print"));
commandRepository.put(OPEN_NOTIFICATIONS,
postC("/session/:sessionId/appium/device/open_notifications"));
commandRepository.put(PRESS_KEY_CODE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class AndroidDriver<T extends WebElement>
extends AppiumDriver<T>
implements PressesKeyCode, HasNetworkConnection, PushesFiles, StartsActivity,
FindsByAndroidUIAutomator<T>, LocksAndroidDevice, HasAndroidSettings, HasDeviceDetails,
HasSupportedPerformanceDataType {
HasSupportedPerformanceDataType, AuthenticatesByFinger {

private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,19 @@ public class AndroidMobileCommandHelper extends MobileCommand {
IS_LOCKED, ImmutableMap.<String, Object>of());
}

/**
* This method forms a {@link java.util.Map} of parameters for the
* finger print authentication invocation.
*
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
* @return a key-value pair. The key is the command name. The value is a
* {@link java.util.Map} command arguments.
*/
public static Map.Entry<String, Map<String, ?>> fingerPrintCommand(int fingerPrintId) {
return new AbstractMap.SimpleEntry<>(FINGER_PRINT,
prepareArguments("fingerprintId", fingerPrintId));
}

/**
* This method forms a {@link java.util.Map} of parameters for the
* notification opening.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.appium.java_client.android;

import static io.appium.java_client.android.AndroidMobileCommandHelper.fingerPrintCommand;

import io.appium.java_client.CommandExecutionHelper;
import io.appium.java_client.ExecutesMethod;

public interface AuthenticatesByFinger extends ExecutesMethod {

/**
* Authenticate users by using their finger print scans on supported emulators.
*
* @param fingerPrintId finger prints stored in Android Keystore system (from 1 to 10)
*/
default void fingerPrint(int fingerPrintId) {
CommandExecutionHelper.execute(this, fingerPrintCommand(fingerPrintId));
}
}
140 changes: 140 additions & 0 deletions src/test/java/io/appium/java_client/android/FingerPrintTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.appium.java_client.android;

import static io.appium.java_client.MobileBy.AndroidUIAutomator;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.openqa.selenium.By.id;

import io.appium.java_client.remote.MobileCapabilityType;
import io.appium.java_client.service.local.AppiumDriverLocalService;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.remote.DesiredCapabilities;

public class FingerPrintTest {
private static AppiumDriverLocalService service;
private static AndroidDriver<AndroidElement> driver;

private static void initDriver() {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "Android Emulator");
capabilities.setCapability("appPackage", "com.android.settings");
capabilities.setCapability("appActivity", "Settings");
driver = new AndroidDriver<>(service.getUrl(), capabilities);
driver.manage().timeouts().implicitlyWait(15, SECONDS);
}

/**
* initialization.
*/
@BeforeClass public static void beforeClass() {
service = AppiumDriverLocalService.buildDefaultService();
service.start();

if (service == null || !service.isRunning()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

service cannot be null here

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mykola-mokhnach it was copied from BaseAndroidTest and for sure it can not be null here :)

throw new ExceptionInInitializerError("An appium server node is not started!");
}
}

/**
* finishing.
*/
@AfterClass public static void afterClass() {
if (service != null) {
service.stop();
}
}

private AndroidElement findElementByText(String text) {
return driver.findElements(id("android:id/title")).stream().filter(androidElement ->
text.equals(androidElement.getText())).findFirst()
.orElseThrow(() ->
new NoSuchElementException(String.format("There is no element with the text '%s'", text)));
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was so used to localization testing that my mind automatically blocked using text to find an element :)
using text is much better and easier in this case 👍


private void clickNext() {
driver.findElementById("com.android.settings:id/next_button").click();
}

private void clickOKInPopup() {
driver.findElementById("android:id/button1").click();
}

private void enterPasswordAndContinue() {
driver.findElementById("com.android.settings:id/password_entry")
.sendKeys("1234\n");
}

private void clickOnSecurity() {
driver.findElement(AndroidUIAutomator("new UiScrollable(new UiSelector()"
+ ".scrollable(true)).scrollIntoView("
+ "new UiSelector().text(\"Security\"));")).click();
}

/**
* enable system security which is required for finger print activation.
*/
@Before public void before() throws Exception {
initDriver();
clickOnSecurity();
findElementByText("Screen lock").click();
findElementByText("PIN").click();
enterPasswordAndContinue();
enterPasswordAndContinue();
clickNext();
}

/**
* add a new finger print to security.
*/
@Test public void fingerPrintTest() {
findElementByText("Fingerprint").click();
clickNext();
enterPasswordAndContinue();
clickNext();

driver.fingerPrint(2);
try {
driver.findElementById("com.android.settings:id/next_button").click();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be clickNext()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Will improve today.

} catch (Exception e) {
Assert.fail("fingerprint command fail to execute");
}
}

/**
* disabling pin lock mode.
*/
@After public void after() throws InterruptedException {
driver.quit();

initDriver();
clickOnSecurity();

findElementByText("Screen lock").click();

enterPasswordAndContinue();
findElementByText("None").click();
clickOKInPopup();
driver.quit();
}
}