How to use Interface Keyboard class of org.openqa.selenium.interactions package

Best Selenium code snippet using org.openqa.selenium.interactions.Interface Keyboard

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1package org.fluentlenium.core.action;
2
3import org.openqa.selenium.Keys;
4import org.openqa.selenium.WebDriver;
5import org.openqa.selenium.WebElement;
6import org.openqa.selenium.interactions.HasInputDevices;
7import org.openqa.selenium.interactions.Keyboard;
8
9/**
10 * Execute actions with the keyboard.
11 */
12public class KeyboardActions {
13    private final WebDriver driver;
14
15    /**
16     * Creates a new object to execute actions with the keyboard, using given selenium driver.
17     *
18     * @param driver selenium driver
19     */
20    public KeyboardActions(WebDriver driver) {
21        this.driver = driver;
22    }
23
24    /**
25     * Get selenium interactions actions.
26     *
27     * @return selenium actions
28     */
29    protected org.openqa.selenium.interactions.Actions actions() {
30        return new org.openqa.selenium.interactions.Actions(driver);
31    }
32
33    /**
34     * Basic keyboard operations
35     *
36     * @return low level interface to control the keyboard
37     * @deprecated Use {@link KeyboardActions#keyDown(Keys)} and {@link KeyboardActions#keyUp(Keys)}
38     * and {@link KeyboardActions#sendKeys(CharSequence...)} instead
39     */
40    @Deprecated
41    public Keyboard basic() {
42        return ((HasInputDevices) driver).getKeyboard();
43    }
44
45    /**
46     * Performs a modifier key press. Does not release the modifier key - subsequent interactions
47     * may assume it's kept pressed.
48     * Note that the modifier key is <b>never</b> released implicitly - either
49     * <i>keyUp(theKey)</i> or <i>sendKeys(Keys.NULL)</i>
50     * must be called to release the modifier.
51     *
52     * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}. If the
53     *               provided key is none of those, {@link IllegalArgumentException} is thrown.
54     * @return this object reference to chain calls
55     * @see org.openqa.selenium.interactions.Actions#keyDown(CharSequence)
56     */
57    public KeyboardActions keyDown(Keys theKey) {
58        actions().keyDown(theKey).perform();
59        return this;
60    }
61
62    /**
63     * Performs a modifier key release. Releasing a non-depressed modifier key will yield undefined
64     * behaviour.
65     *
66     * @param theKey Either {@link Keys#SHIFT}, {@link Keys#ALT} or {@link Keys#CONTROL}.
67     * @return this object reference to chain calls
68     * @see org.openqa.selenium.interactions.Actions#keyUp(CharSequence)
69     */
70    public KeyboardActions keyUp(Keys theKey) {
71        actions().keyUp(theKey).perform();
72        return this;
73    }
74
75    /**
76     * Sends keys to the active element. This differs from calling
77     * {@link WebElement#sendKeys(CharSequence...)} on the active element in two ways:
78     * <ul>
79     * <li>The modifier keys included in this call are not released.</li>
80     * <li>There is no attempt to re-focus the element - so sendKeys(Keys.TAB) for switching
81     * elements should work. </li>
82     * </ul>
83     *
84     * @param keysToSend The keys.
85     * @return A self reference.
86     * @see org.openqa.selenium.interactions.Actions#sendKeys(CharSequence...)
87     */
88    public KeyboardActions sendKeys(CharSequence... keysToSend) {
89        actions().sendKeys(keysToSend).perform();
90        return this;
91    }
92}
93
Full Screen
copy
1/*
2Copyright 2012 Selenium committers
3Copyright 2012 Software Freedom Conservancy
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9     http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16*/
17package org.openqa.selenium.interactions;
18
19import org.junit.runner.RunWith;
20import org.junit.runners.Suite;
21import org.openqa.selenium.interactions.touch.TouchDoubleTapTest;
22import org.openqa.selenium.interactions.touch.TouchFlickTest;
23import org.openqa.selenium.interactions.touch.TouchLongPressTest;
24import org.openqa.selenium.interactions.touch.TouchScrollTest;
25import org.openqa.selenium.interactions.touch.TouchSingleTapTest;
26
27@RunWith(Suite.class)
28@Suite.SuiteClasses({
29    ActionsTest.class,
30    BasicKeyboardInterfaceTest.class,
31    BasicMouseInterfaceTest.class,
32    CombinedInputActionsTest.class,
33    CompositeActionTest.class,
34    IndividualKeyboardActionsTest.class,
35    IndividualMouseActionsTest.class,
36    TouchDoubleTapTest.class,
37    TouchFlickTest.class,
38    TouchLongPressTest.class,
39    TouchScrollTest.class,
40    TouchSingleTapTest.class
41})
42public class InteractionTests {
43}
44
Full Screen
copy
1<!-- hibernate -->
2<hibernate-core.version>3.3.2.GA</hibernate-core.version>
3<hibernate-annotations.version>3.4.0.GA</hibernate-annotations.version>
4<hibernate-commons-annotations.version>3.1.0.GA</hibernate-commons-annotations.version>
5<hibernate-entitymanager.version>3.4.0.GA</hibernate-entitymanager.version>
6
Full Screen
copy
1@Entity
2@Access(AccessType.FIELD)
3public class SomeEntity implements Serializable
4{
5...
6}
7
Full Screen
copy
1<hibernate-mapping package="org.nkl.model" default-access="field">
2  <class name="FieldBean" table="FIELD_BEAN">
3    <id name="id">
4      <generator class="sequence" />
5    </id>
6    <property name="message" />
7  </class>
8</hibernate-mapping>
9
10<hibernate-mapping package="org.nkl.model" default-access="property">
11  <class name="PropBean" table="PROP_BEAN">
12    <id name="id">
13      <generator class="sequence" />
14    </id>
15    <property name="message" />
16  </class>
17</hibernate-mapping>
18
Full Screen
copy
1@Test
2public void testFieldBean() {
3    Session session = sessionFactory.openSession();
4    Transaction tx = session.beginTransaction();
5    FieldBean fb = new FieldBean("field");
6    Long id = (Long) session.save(fb);
7    tx.commit();
8    session.close();
9
10    session = sessionFactory.openSession();
11    tx = session.beginTransaction();
12    fb = (FieldBean) session.load(FieldBean.class, id);
13    System.out.println(fb.getId());
14    tx.commit();
15    session.close();
16}
17
18@Test
19public void testPropBean() {
20    Session session = sessionFactory.openSession();
21    Transaction tx = session.beginTransaction();
22    PropBean pb = new PropBean("prop");
23    Long id = (Long) session.save(pb);
24    tx.commit();
25    session.close();
26
27    session = sessionFactory.openSession();
28    tx = session.beginTransaction();
29    pb = (PropBean) session.load(PropBean.class, id);
30    System.out.println(pb.getId());
31    tx.commit();
32    session.close();
33}
34
Full Screen
copy
1Hibernate: 
2    call next value for hibernate_sequence
3Hibernate: 
4    insert 
5    into
6        FIELD_BEAN
7        (message, id) 
8    values
9        (?, ?)
10Hibernate: 
11    select
12        fieldbean0_.id as id1_0_,
13        fieldbean0_.message as message1_0_ 
14    from
15        FIELD_BEAN fieldbean0_ 
16    where
17        fieldbean0_.id=?
180
19Hibernate: 
20    call next value for hibernate_sequence
21Hibernate: 
22    insert 
23    into
24        PROP_BEAN
25        (message, id) 
26    values
27        (?, ?)
281
29
Full Screen
copy
1@Entity
2public class Person {
3
4  @Column("nickName")
5  public String getNickName(){
6     if(this.name != null) return generateFunnyNick(this.name);
7     else return "John Doe";
8  }
9}
10
Full Screen
copy
1public abstract class Foo<T extends Bar> {
2
3    T oneThing;
4    T anotherThing;
5
6    // getters and setters ommited for brevity
7
8    // Lots and lots of implementation regarding oneThing and anotherThing here
9 }
10
Full Screen
copy
1@Override
2public void onResume() {
3    Objects.requireNonNull(getActivity()).getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
4    super.onResume();
5}
6
Full Screen
copy
1
2ViewCompat.setOnApplyWindowInsetsListener(window.decorView.rootView) { _, insets ->
3 
4   //This lambda block will be called, every time keyboard is opened or closed
5
6    
7    val imeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
8    if(imeVisible){ 
9     //Now show-hide bottom navigation accordingly 
10    } 
11
12    insets
13}
14
15
Full Screen
copy
1<activity android:name=".Activities.OrderSceenWithOrder" 
2   android:windowSoftInputMode="adjustPan"
3    >
4    <intent-filter>
5        <action android:name="android.intent.action.MAIN" />
6
7        <category android:name="android.intent.category.LAUNCHER" />
8    </intent-filter>
9</activity>
10
Full Screen
copy
1 <activity
2        android:name=".YourActivity"
3        android:windowSoftInputMode="adjustResize"/>
4
Full Screen
copy
1 private boolean keyboardListenersAttached = false;
2private ViewGroup rootLayout;
3
4
5       @Override
6public View onCreateView(LayoutInflater inflater, ViewGroup container,
7                         Bundle savedInstanceState) {
8    // Inflate the layout for this fragment
9    View view = inflater.inflate(R.layout.fragment_settings, container, false);
10
11
12    rootLayout = (ViewGroup) view.findViewById(R.id.settings_layout);
13    attachKeyboardListeners();
14
15    return view;
16}
17
18
19
20    private ViewTreeObserver.OnGlobalLayoutListener keyboardLayoutListener = new 
21    ViewTreeObserver.OnGlobalLayoutListener() {
22    @Override
23    public void onGlobalLayout() {
24        int heightDiff = rootLayout.getRootView().getHeight() - 
25        rootLayout.getHeight();
26        if (getActivity() != null) {
27            int contentViewTop = 
28           getActivity().getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
29            LocalBroadcastManager broadcastManager = 
30            LocalBroadcastManager.getInstance(getContext());
31            Rect r = new Rect();
32            rootLayout.getWindowVisibleDisplayFrame(r);
33            int screenHeight = rootLayout.getRootView().getHeight();
34
35            // r.bottom is the position above soft keypad or device button.
36            // if keypad is shown, the r.bottom is smaller than that before.
37            int keypadHeight = screenHeight - r.bottom;
38            if (keypadHeight > screenHeight * 0.15) {
39                onHideKeyboard();
40
41                Intent intent = new Intent("KeyboardWillHide");
42                broadcastManager.sendBroadcast(intent);
43            } else {
44                int keyboardHeight = heightDiff - contentViewTop;
45                onShowKeyboard(keyboardHeight);
46
47                Intent intent = new Intent("KeyboardWillShow");
48                intent.putExtra("KeyboardHeight", keyboardHeight);
49                broadcastManager.sendBroadcast(intent);
50            }
51        }else {
52
53        }
54    }
55};
56
57protected void onShowKeyboard(int keyboardHeight) {
58    System.out.println("keyboard is shown");
59    dashboardActivity.bottomNavigationView.setVisibility(View.VISIBLE);
60    dashboardActivity.fab.setVisibility(View.VISIBLE);
61
62}
63protected void onHideKeyboard() {
64    System.out.println("keyboard is hide");
65    dashboardActivity.bottomNavigationView.setVisibility(View.INVISIBLE);
66    dashboardActivity.fab.setVisibility(View.INVISIBLE);
67}
68
69protected void attachKeyboardListeners() {
70    if (keyboardListenersAttached) {
71        return;
72    }
73
74    rootLayout.getViewTreeObserver().addOnGlobalLayoutListener(keyboardLayoutListener);
75    keyboardListenersAttached = true;
76}
77
Full Screen
copy
1<activity
2        android:name=".Activity.DashboardActivity"
3        android:windowSoftInputMode="adjustResize"
4
5         />
6
Full Screen
copy
1<activity
2    android:name=".MainActivity"
3    android:windowSoftInputMode="stateAlwaysHidden|adjustPan" />
4
Full Screen
copy
1getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_NOTHING);
2
Full Screen
copy
1 <activity android:name=".MainActivity"
2        android:windowSoftInputMode="adjustPan">
3
Full Screen
copy
1<activity android:windowSoftInputMode="adjustPan"/>
2
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used methods in Interface-Keyboard

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)