I have Linux application written with ncurses. I'm trying to automate it using pexpect but without success.
I can spawn an application and can work with output but I can't send arrow down key:
import pexpect
import time
import sys, os
os.environ['LINES'] = "25"
os.environ['COLUMNS'] = "80"
child=pexpect.spawn("my_ncurses_app", maxread=4000, encoding="utf-8")
child.logfile=sys.stdout
child.setwinsize(25,80)
KEY_DOWN = '\033[B'
#close button appears on screen, After that I want to press down key twice and enter
child.expect("Close")
#ncurses_app sees KEY_DOWN as 3 different keys \033, [, B
child.send(KEY_DOWN)
child.send(KEY_DOWN)
child.sendline()
#ncurses_app sees enter as Int(10)
It works perfect for other CLI applications, but not for my.
Debugging shows what instead of 1 arrow down symbol application sees 3 different keys.
How I can send KEY_DOWN as one symbol? Possibly I should use smth other instead of pexpect, smth with low-level interaction with processes?
Related
I want to test how my app reacts to numpad keys. I found in https://www.w3.org/TR/webdriver/ specs that for example for Numpad Home (with location = DOM_KEY_LOCATION_NUMPAD = 3) a symbol \uE057 should be used. However, it doesn't work for me: I get Home with default location (0), moreover, event.code is empty. It gives me a different result when I physically press NUMPAD7 button with Num Lock off: it that case, I get correct location 3 and event.code is Numpad7.
var options = FirefoxOptions();
options.setLogLevel(FirefoxDriverLogLevel.TRACE);
var driver = FirefoxDriver(options);
driver.navigate().to("https://keycode.info/");
driver.findElementByTagName("body").sendKeys("\uE057");
So how can I send such a key? I'm now thinking of manual recording of generated events when I physically press a key, and then sending these events via Selenium's execution of JS script. However, I haven't tried it yet; maybe there is a better way to do it in Selenium; maybe there is another framework that allows it better.
By the way, I've filed a similar ticket in geckodriver because it looks like a bug of webdriver to me...
\ue01d is the unicode for NUmberpad3
python code:
from selenium import webdriver
from selenium import webdriver
options = webdriver.ChromeOptions()
driver = webdriver.Chrome()
driver.get("https://keycode.info/")
driver.find_element_by_tag_name("body").send_keys("\ue01d")
input()
you can use https://www.javadoc.io/doc/org.seleniumhq.selenium/selenium-api/latest/org/openqa/selenium/Keys.html to send keys instead of sending unicode directly
In firefox this is will work if you use action chain:
ActionChains.send_keys("\ue01d").perform()
I'm trying out Karate and have a use case where I need to trigger a search in a search box and there is no button to trigger the search, so it needs to be triggered via the enter key.
I have tried multiple different flavours of trying to provide Key.ENTER to the input to get it to work but none of them triggers it.
I am using the latest binary with a very basic feature file (altered to use google rather than an internal app URL):
Feature: Trigger search with enter
Background:
* configure driver = { type: 'chrome'}
Scenario: Trigger Google search with enter
Given driver 'https://google.com'
# 1: Attempting to search with enter as an array argument
When input('input[name=q]', ['karate dsl', Key.ENTER])
# 2: Attempting to search with enter as a second command
#When input('input[name=q]', 'karate dsl')
#When input('input[name=q]', Key.ENTER)
# 3: Attempting to search using similar approach to 1 but with a submit
#When submit().input('input[name=q]', ['karate dsl', Key.ENTER])
Then waitFor('{h3}intuit/karate: Test Automation Made Simple - GitHub')
When using any of these approaches (by running ./karate <PATH_TO_ABOVE_FEATURE_FILE>) the search results page never loads so the result (the h3) can never be found...what am I doing wrong?
This is a bug for type chrome. It will actually work for type chromedriver.
Opened an issue: https://github.com/intuit/karate/issues/1192
For now please workaround by using a click on the appropriate button / control etc.
So I have a game client with 2 input fields: id pass and 1 button: login.
My login credentials are: $id=1234 and $pass=a_bCd.
I'm using AutoIT scripting to automate the login process (my script automatically inputs the id and pass in the login fields) and my AutoLogin() function looks like:
send($id + "{tab}")
Sleep(10)
send($pass + "{enter}")
Sometimes it works fine, but sometimes my script introduces 1234a- or 1234a_ in the ID field and the rest of the characters in the pass field. I tried many solutions like controlsend("Game","","","1234{tab}a_bCd{enter}"), or changing sleep() values, etc. but the input still goes wrong sometimes. Figured the send delay or sleep would have the problem, still don't know what to do.
Manually inserting the id and pass works properly. What would be a good solving of this problem? Thanks
I have had the same troubles with send. This post on autoitscript.com may help you with WinAPI_Keybd_Event() (documentation here):
#include <WinAPISys.au3>
_WinAPI_Keybd_Event(0x11, 0) # Push CTRL down
_WinAPI_Keybd_Event(0x11, 2) # Lift CTRL up again
This helped me with my problems.
NOTE: Sometimes, when your script breaks because of an error, it may happen that one of the keys on your keyboard is still pressed (i.e. pushed down but never lifted up). I use the on-screen keyboard that is integrated in Windows for these moments...
2 Solutions:
You add Strings with &:
send($id & "{tab}")
send($pass & "{enter}")
If that does not work just separate it:
send($id)
send("{tab}")
send($pass)
send("{enter}")
And you don't need that sleep
Several commands on linux require a keyboard input to complete the process, in particular when a password is requested, or a yes/no confirmation.
I have no problems issuing the command in python and getting the results, but how do I automatically answer/spoof the proper answer to a child prompt and get the return code from os.system ?
You might consider using the pexpect library, which does exactly what you're asking for.
import pexpect
>>> child = pexpect.spawn('ssh user#myhost')
>>> child.expect('password:')
>>> child.sendline('123password')
>>> child.expect('Welcome to myhost, user')
>>> print 'successfully authenticated'
I am attempting to move a character using the "asdw" keys in a game, but I cannot find a way to constantly input data without pressing return. I have seen that on windows there is a module called msvcrt, which has a getch function, so I am wondering if there is a way to simulate this in OSX, or more simply to just constantly input data from the keyboard.
Try the curses library:
http://docs.python.org/py3k/library/curses.html
Curses is a library for controlling the terminal, and includes features such as drawing box shapes as well. It's available on any POSIX-compatible system, which includes Mac OS X and GNU/Linux.
Here's an example:
import curses
import time
# Turn off line buffering
curses.cbreak()
# Initialize the terminal
win = curses.initscr()
# Make getch() non-blocking
win.nodelay(True)
while True:
key = win.getch()
if key != -1:
print('Pressed key', key)
time.sleep(0.01)
You can use Turtle to do something like this:
import turtle
Sc = turtle.Screen()
Sc.setup(width=0, height=0) #this hides turtle's windows
def a(): #that's the function that you want to run when the key is pressed
#code here
Sc.listen() #this tells the program to listen
for a keypress
Sc.onkey("#The key here", #the function call here) #this tells the program
What function to call when a certain key is pressed
# An example of pressing the key "w"
Sc.onkey("w", a)