Android MonkeyRunner unable to enter text - testing

I recently started using MonkeyRunner to test the UI of my Android application (I am also using Espresso but wanted to play around with MonkeyRunner). The problem that I am having is that I am unable to enter text into EditText fields using the automation script.
The script navigates through my app perfectly but it doesn't seem to actually enter any text on call of the MonkeyRunner.type() command.
Please find my script below.
from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice
from com.android.monkeyrunner.easy import EasyMonkeyDevice, By
import commands
import sys
import os
# starting the application and test
print "Starting the monkeyrunner script"
# connection to the current device, and return a MonkeyDevice object
device = MonkeyRunner.waitForConnection()
easy_device = EasyMonkeyDevice(device)
apk_path = device.shell('pm path com.mysample
if apk_path.startswith('package:'):
print "application installed."
else:
print "not installed, install APK"
device.installPackage('/MySample/MySample.apk')')
package ="com.mysample"
activity = ".SampleActivity"
print "Package: " + package + "Activity: " + activity
print "starting application...."
device.startActivity(component=package + '/' + activity)
print "...component started"
device.touch(205,361, "DOWN_AND_UP")
device.type("This is sample text")
MonkeyRunner.sleep(1)
result = device.takeSnapshot()
result.writeToFile("images/testimage.png",'png')
As you can see from the script above the text This is sample text should be placed in the EditText box. Both the emulator and screenshot that is taken show no text in the text field.
Am I missing a step or just doing something incorrectly?
Any help would be greatly appreciated!

I would rather use AndroidViewClient/culebra to simplify the task.
Basically, you can connect your device with adb and then run
culebra -VC -d on -t on -o myscript.py
The script obtains references to all of the visible Views. Edit the script and add at the end
no_id10.type('This is sample text')
no_id10.writeImageToFile('/tmp/image.png')
No need to worry about View coordinates, no need to touch and type, no need to add sleeps, etc.
NOTE: this is using no_id10 as an example, the id for your EditText could be different

First of all, I would not use the MonkeyRunner.sleep command, but I would rather use the time package and the time.sleep command. Just import the package
import time
and you should be good to go.
Moreover, I suggest you should wait some time between device.touch and device.type. Try with
device.touch(205,361, "DOWN_AND_UP")
time.sleep(1)
device.type("This is sample text")

Related

How to write description of a test step in Katalon Studio in script mode?

How to write “description” in script mode using a variable?
For example:
'Verifing the headings ’ + actualtextvariable
WebUI.verifyMatch(“text”, actualtextvariable, false)
Reference URL for the script mode: https://docs.katalon.com/katalon-studio/tutorials/create_test_case_using_script_mode.html.
Thanks!
I don't think you can do that. I guess the closest thing would be to log the description so you can see it in the console upon execution:
import com.kms.katalon.core.util.KeywordUtil
KeywordUtil.logInfo('Verifing the headings ’ + actualtextvariable)

User input produces blank file with no result

I am playing around with Selenium to get screenshots of websites to view them safely.
The original code I found and tweaked was like this and works.
from selenium import webdriver
br = webdriver.PhantomJS()
br.get('http://www.google.com')
br.save_screenshot('screenshot.png')
br.quit
It gives you a screenshot of the website
I wanted to get user input so that I did not have the VI the file overtime I need a screenshot of a URL this is what I changed the code too.
#!/usr/bin/python
import re
import sys
from selenium import webdriver
br = webdriver.PhantomJS()
br.get_user_input =raw_input('Enter URL:')
br.save_screenshot('screenshot.png')
br.quit
Now it ask for the URL and you input it in the program runs and finishes and even creates the screenshot.png file but its blank
try to use a chrome canary (if you need headless browser). you don't have to use a selenium.
make_screen = '''#!/usr/bin/env bash
test -f ./chrome-linux/chrome && echo "chrome exists" || unzip chrome-linux.zip
./chrome-linux/chrome --headless --disable-gpu --virtual-time-budget=7000 --hide-scrollbars --screenshot=dir/screens/{screen_prod}.png --window-size=1200,2000 {link_prod}'''.format(screen_prod = screen_prod, link_prod = link_prod)
Here is the Answer to your Question:
There is a small bug in your code. You havn't called the get() method with your WebDriver instance br.
As a solution, (to avoid Law of Demeter) I have broken up that line in to two. Took the url from the user as an input in the first line. In the next line passed the url as an argument to get() method. Next it takes a proper snapshot and saves it within Screenshots sub-directory. Here is the working code block :
from selenium import webdriver
br = webdriver.PhantomJS(executable_path="C:\\Utility\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe")
user_input = input('Enter URL : ')
br.get(str(user_input))
br.save_screenshot('./Screenshots/my_next_screenshot.png')
br.quit
The output on my console is:
Enter URL : http://google.com
Process finished with exit code 0
Let me know if this Answers your Question.

SSH Paramiko-Python interactive script-Need to highlight a line from the list and select it

I am using paramiko for ssh interactive commands. I am able to successfully send all the required commands. I am stuck on one issue. i need somebody help to automate this task.
Issue is on output, i will get 4 options, from this options, i need to move to the required option by using arrow keys and then press enter button to select that option.
Please let me know if anybody knows about this.
import paramiko
import time
import os
ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('server',port=22,username='user',password='pass123')
print("connected to the linux machine from windows machine.")
channel=ssh.invoke_shell()
channel_data = str()
while True:
if channel.recv_ready():
channel_data += channel.recv(9999).decode(encoding='utf_8', errors='strict')
os.system('cls')
print("##### Device Output #####")
print("\n",channel_data)
print("\n #####################")
else:
continue
time.sleep(5)
if channel_data.endswith('[root#home ~]# '):
channel.send('somecommand\n')
#highlight and then press enter button to select that option. please help for below code
ifelse channel_data.endswith('I am trying to choose this option from the list'):
channel.send('\n')
We need to send the keycode. for down arrow key, "[B 1b 5b 42" is key code.
Below is the link for complete list of keycode mapping.
http://www.comptechdoc.org/os/linux/howlinuxworks/linux_hlkeycodes.html

Running external selenium script from Groovy[SOAPUI]

I see online that the way to run a simple python file from groovy is:
def cmdArray2 = ["python", "/Users/test/temp/hello.py"]
def cmd2 = cmdArray2.execute()
cmd2.waitForOrKill(1000)
log.info cmd2.text
If hello.py contains - Print "Hello". It seems to work fine
But when I try to run a .py file containing the below Selenium code nothing happens.
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.PhantomJS(executable_path=r'C:\phantomjs\bin\phantomjs.exe')
driver.get("http://www.google.com") # Load page
# 1 & 2
title = driver.title
print title, len(title)
driver.quit()
Any help would be appreciated.
FYI - I have tried using all the browsers including headless browsers but no luck.
Also, I am able to run the selenium script fine from the command line. But when I run from the SOAPUI, I get no errors, the script runs and I do not see anything in the log
Most likely you do not see any errors from your python script because they are printed to stderr not stdout (as you expected when called cmd2.text). Try this groovy script to check error messages from python script stderr
def cmdArray2 = ["python", "/Users/test/temp/hello.py"]
def process = new ProcessBuilder(cmdArray2).redirectErrorStream(true).start()
process.inputStream.eachLine {
log.warn(it)
}
process.waitFor()
return process.exitValue()
Another thing you might want to try is using selenium directly from groovy without calling external python script.

Create Dialog Box in Blender using C or Python

How to make a dialog box (three options like quit/OK/Cancel) in blender and processing the text entered through python or in C. I'm unable to find any good tutorial on this. Any help....?
A quick and dirty way is to use zenity command (should be included by default in any python distribution). Try this short example script, it works in my Blender 2.69 on Ubuntu 14.04.
import bpy # bpy or bge does not matter
import subprocess as SP
# call an OS subprocess $ zenity --entry --text "some text"
# (this will ask OS to open a window with the dialog)
res=SP.Popen(['zenity','--entry','--text',
'please write some text'], stdout=SP.PIPE)
# get the user input string back
usertext=str(res.communicate()[0][:-1])
# adjust user input string
text=usertext[2:-1]
print("I got this text from the user: %s"%text)
See the zenity --help for more complex dialogs
blender doesn't offer things like dialogs.
Answers to This previous question on external modules may be helpful.
class DialogOperator(bpy.types.Operator)
bl_idname = "object.dialog_operator"
bl_label = "Save Before You QUIT!"
def execute(self, context):
message = " You didn't saved yet "
self.report({'INFO'}, message)
print(message)
return {'FINISHED'}
def invoke(self, context, event):
return context.window_manager.invoke_props_dialog(self)
class DialogPanel(bpy.types.Panel)
bl_label = "Dialog"
bl_space_type = "VIEW_3D"
bl_region_type = "UI"
def draw(self, context):
self.layout.operator("object.dialog_operator")
But this is only for creating a dialog window. after this have to insert buttons in this code.If anyone known this try to post the answer. At the same time I'm also trying to sort out this.