Appium - Find Element by Image - appium-android

I need some help to code in python with Appium to find element by image and click on the image.
If someone has a sample working code please share.
I tried following code:
import base64
from appium.webdriver.common.mobileby import MobileBy
with open(r"C:\Users\ADMIN\Desktop\flipkart.png", "rb") as image_file:
image_base64 = base64.b64encode(image_file.read()).decode("utf-8")
caps = {
"platformName": "Android",
"appium:deviceName": "Galaxy M21",
"appium:udid": "RZ8N50CSZWT",
"noReset": True
}
driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", caps)
driver.implicitly_wait(20)
element = driver.find_element_by_image(image_base64)
element.click()
driver.quit()
`

Related

How can I create a GtkImage from a GIcon witha a fallback?

I have a Gio.Icon (or GIcon in C, I'm using pygobject). Right now I'm using the following code to create a Gtk.Image from the Gio.Icon:
image = icon and Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48, use_fallback=True)
The problem is, that the Gio.Icon isn't garanteed to have a valid icon name/path and when it doesn't it shows a broken image icon. I would like to fall back to using a different icon I know exists if the supplied Gio.Icon is invalid. Is there some way to know if the Gio.Icon is invalid, or if the Gtk.Image would show as a broken image?
EDIT
A minimal example:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
win = Gtk.Window()
win.connect('destroy', Gtk.main_quit)
icon = Gio.Icon.new_for_string('gnome-garbage')
fallback_icon = Gio.Icon.new_for_string('folder')
image = Gtk.Image(gicon=icon, icon_size=Gtk.IconSize.DIALOG, pixel_size=48)
win.add(image)
win.show_all()
Gtk.main()
I found an answer in the GtkImage documentation:
If the file isn’t loaded successfully, the image will contain a “broken image” icon similar to that used in many web browsers. If you want to handle errors in loading the file yourself, for example by displaying an error message, then load the image with gdk_pixbuf_new_from_file(), then create the GtkImage with gtk_image_new_from_pixbuf().
Although, in my case I actually need to use a GtkIconTheme to get a PixBuf instead of gdk_pixbuf_new_from_file:
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gio
win = Gtk.Window()
win.connect('destroy', Gtk.main_quit)
def load_icon(icon):
info = Gtk.IconTheme.get_default().lookup_by_gicon(icon, 48, Gtk.IconLookupFlags.FORCE_SIZE)
if info:
return info.load_icon()
icon = Gio.Icon.new_for_string('gnome-garbage')
fallback_icon = Gio.Icon.new_for_string('folder')
pixbuf = load_icon(icon) or load_icon(fallback_icon)
image = Gtk.Image.new_from_pixbuf(pixbuf)
win.add(image)
win.show_all()
Gtk.main()

I am unable to upload attachment using SeleniumWebDriver

This is my Code
driver.findElement(By.id("ctl00_ContentPlaceHolder1_cbpAssociationNew_panelAssnDetailAdd_Add_Photo_Browse0")).click();
Thread.sleep(2000);
StringSelection ss= new StringSelection("C:\\Users\\ns10\\Desktop\\download.jpg");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
Robot robo=new Robot();
robo.delay(1000);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
robo.keyPress(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
When I run the above code in Eclipse, it clicks on the File upload; then the windows pop up comes but doesn't select the file which I mentioned. It was just idle.
Could someone please help me with this.
If possible try this first,
driver.findElement(By.id("ctl00_ContentPlaceHolder1_cbpAssociationNew_panelAssnDetailAdd_Add_Photo_Browse0")).sendKeys("C:\\Users\\ns10\\Desktop\\download.jpg");
This is a noted way to upload file via selenium webdriver as of now, where you don't have to bother about clicking on the File Upload button and then using Robot class to upload image. Just sending the path of the file to upload will accomplish the Upload.
If it still doesn't work, then below is the alternative way by using Robot and Sikuli (combined) to accomplish the task:-
Problem:-
Sometimes, the Robot class types the "attachment_path" into the "File Name" field of textbox but doesn't presses 'Enter key' thereafter for the file to upload.
Solution:-
To avert that, I've used Sikuli to click on the Open button, which does infact uploads the file then;
(Note:- You have to take a screenshot of Open button in the window's dialog box, that comes up after clicking on the "File Upload button".
First of all download sikuli-api standalone jar. And, add it to the build path of your project. Then, add the below code for the file upload:-
//Clicking on the File Upload button
driver.findElement(By.id("ctl00_ContentPlaceHolder1_cbpAssociationNew_panelAssnDetailAdd_Add_Photo_Browse0")).click();
//Copying the path of the file to the clipboard
StringSelection attachment = new StringSelection("C:\\Users\\ns10\\Desktop\\download.jpg"); //path to the attachment
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(attachment, null);
//Pasting the contents of clipboard in the field "File name" of the Window Pop-up
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
try{
ScreenRegion screen = new DesktopScreenRegion(); //screen is the default screen region which corresponds to the entire screen on your default monitor.
Target target = new ImageTarget(new File("<PUT HERE THE PATH FOR THE OPEN BUTTON'S IMAGE>"));
ScreenRegion screen_web = screen.wait(target,5000);
screen_web = screen.find(target); //screen_web holds the screen region occupied by the "Open" button that was found by Sikuli's image recognition engine.
if(screen_web != null)
{
Mouse mouse = new DesktopMouse(); // Create a mouse object
mouse.click(screen_web.getCenter()); // Use the mouse object to click on the center of the target region, i.e., the Open button
}else{
throw new Throwable();
}
}catch(Throwable e){
System.err.println("The Open file button wasn't clicked!!" + e.getMessage() + "\n---------------------");
}
Code for Uploading in Tiny Pic.com :-
package pack_ads;
import java.awt.AWTException;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.sikuli.api.DesktopScreenRegion;
import org.sikuli.api.ImageTarget;
import org.sikuli.api.ScreenRegion;
import org.sikuli.api.Target;
import org.sikuli.api.robot.Mouse;
import org.sikuli.api.robot.desktop.DesktopMouse;
public class Testing_TinyPicUpload {
public static void main(String[] args) throws AWTException, InterruptedException {
WebDriver driver = new FirefoxDriver(); //Opens a firefox browser instance
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); //Timeout of 30 seconds given
driver.manage().window().maximize(); //Maximizing the window
//Navigating to tiny pic site
driver.get("http://tinypic.com/");
//Uploading photo
driver.findElement(By.xpath("//input[#id='the_file']")).click(); //Locating the attach button
//Copying the path of the file to the clipboard
StringSelection photo = new StringSelection("D:\\\\Images\\default.jpg"); //Putting the path of the image to upload
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(photo, null);
//Pasting the contents of clipboard in the field "File name" of the Window Pop-up
Thread.sleep(5000);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
try{
ScreenRegion screen = new DesktopScreenRegion(); //screen is the default screen region which corresponds to the entire screen on your default monitor.
Target target = new ImageTarget(new File("Tinyimages\\Open_file.png")); //Made a folder in my project as Tinyimages and put the image of Open_file in there.
ScreenRegion screen_web = screen.wait(target,5000);
screen_web = screen.find(target); //screen_web holds the screen region occupied by the "Open" button that was found by Sikuli's image recognition engine.
if(screen_web != null)
{
Mouse mouse = new DesktopMouse(); // Create a mouse object
mouse.click(screen_web.getCenter()); // Use the mouse object to click on the center of the target region, i.e., the Open button
}
else{
throw new Throwable();
}
System.out.println("Attachment was successfully done.\n------------------------------\n");
}catch(Throwable e){
System.err.println("The Open file button wasn't clicked!!" + e.getMessage() + "\n------------------------------");
}
}
}
Note: You can download use this image for the "Open" button from here. Change the file path accordingly in the code Target target = new ImageTarget(new File("Tinyimages\\Open_file.png"));
I was finding the same result, that the code would halt and wait when the File Upload window was opened, and would only continue after the window was closed.
As a workaround I put the thread sleep and the robot keypresses in a separate thread that I spawned just before clicking the file upload button. This solved the issue for me.
// Thread class to be instantiated and run just before button click:
//
public class PasteUploadFileThread extends Thread {
String uploadFileName = "";
public PasteUploadFileThread(String filename){
this.uploadFileName = filename;
}
public void run() {
try {
// Pause 3 seconds giving window time to open up
Thread.sleep(3000);
System.out.println("STARTING PasteUploadFileThread for file: " + this.uploadFileName);
StringSelection ss= new StringSelection(this.uploadFileName);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(ss, ss);
Robot robo=new Robot();
robo.delay(2000);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
robo.keyPress(KeyEvent.VK_CONTROL);
robo.keyPress(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_V);
robo.keyRelease(KeyEvent.VK_CONTROL);
robo.delay(2000);
robo.keyPress(KeyEvent.VK_ENTER);
robo.keyRelease(KeyEvent.VK_ENTER);
System.out.println("ENDING PasteUploadFileThread for file: " + this.uploadFileName);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (AWTException e) {
e.printStackTrace();
}
}
}
// Portion of the test class that spawns the thread and then clicks the button
String filepath = "C:\\UploadDocument.txt";
WebElement browseButton = wait60Secs.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//span[contains(.,'Browse')]")));
// Spawn a thread that will do the interacting with the File Upload window.
// This is needed because when we click on the browse button the code halts waiting for the File Upload window to be closed.
// So this thread will go off on it's own and do it's work while this code is paused.
(new PasteUploadFileThread(filepath)).start();
browseButton.click();
// Execution continues here after the Upload window is closed...
// Find the file that was uploaded before continuing
wait60Secs.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//div[contains(.,'UploadDocument.txt')]")));

Change button image in web.py

I have the following code in my python script. How do I change the image of my button instead of the default ugly looking button?
import web
from web import form
render = web.template.render('templates/')
urls = ('/','index')
register_form = form.Form(
form.Textbox("name",size=40, description="Please enter name: "),
form.Button("Query", type="submit", description="Query")
)
I was hoping to have an effect similar to this:
< button>< img src="index.png">< /img>< /button>
The html option allows you to enter any html tags. :)
form.Button("Query", type="submit", description="Query" html="<img src="hello.png"/)

Replacing InteractiveShellEmbed with Qt console

I am running embedded ipython console via (simplified):
# the code uses PyQt4, this makes sure it is initialized properly
import IPython.lib.inputhook
qapp=IPython.lib.inputhook.enable_gui(gui='qt4')
# create the embedded terminal
from IPython.frontend.terminal.embed import InteractiveShellEmbed
ipshell=InteractiveShellEmbed()
ipshell()
What would this code look like if I would like to run ipython's Qt console instead of the embedded terminal shell? There are examples of using ipython qtconsole all around, but not how to integrate it into my own code.
There is an example script that works in this question: Embedding IPython Qt console in a PyQt application
Here you can find it for reference:
from IPython.zmq.ipkernel import IPKernelApp
from IPython.lib.kernel import find_connection_file
from IPython.frontend.qt.kernelmanager import QtKernelManager
from IPython.frontend.qt.console.rich_ipython_widget import RichIPythonWidget
from IPython.utils.traitlets import TraitError
from PySide import QtGui, QtCore
import atexit
def event_loop(kernel):
kernel.timer = QtCore.QTimer()
kernel.timer.timeout.connect(kernel.do_one_iteration)
kernel.timer.start(1000*kernel._poll_interval)
def default_kernel_app():
app = IPKernelApp.instance()
app.initialize(['python', '--pylab=qt', '--profile=plask'])
app.kernel.eventloop = event_loop
return app
def default_manager(kernel):
connection_file = find_connection_file(kernel.connection_file)
manager = QtKernelManager(connection_file=connection_file)
manager.load_connection_file()
manager.start_channels()
atexit.register(manager.cleanup_connection_file)
return manager
def console_widget(manager):
try: # Ipython v0.13
widget = RichIPythonWidget(gui_completion='droplist')
except TraitError: # IPython v0.12
widget = RichIPythonWidget(gui_completion=True)
widget.kernel_manager = manager
return widget
def terminal_widget(**kwargs):
kernel_app = default_kernel_app()
manager = default_manager(kernel_app)
widget = console_widget(manager)
# Update namespace
kernel_app.shell.user_ns.update(kwargs)
kernel_app.start()
return widget
# This simply opens qtconsole widged in a new window. But you can find embed it wherever you want
app = QtGui.QApplication([''])
widget = terminal_widget(testing=123)
widget.setWindowTitle("Your console")
widget.show()
app.exec_()

error when opening video in titanium

I've been working in Titanium appaccelerator and now I'm trying to open a video in it.
I've used the following code:
movieWindow.js
function displayVideo()
{
var window = Ti.UI.createWindow({
width:200,
height:300,
});
var activeMovie = Titanium.Media.createVideoPlayer({
url:"respigrandsoupir.mp4",
width:300,
height:200,
top:50,
left:50,
backgroundColor:'#0f0'
});
window.add(activeMovie);
activeMovie.play();
return window;
}
My video respigrandsoupir.mp4 is under the Resource folder. The problem is that when trying to run this method I get the following error:
[WARN] Exception in event callback. { expressionBeginOffset = 159;
expressionCaretOffset = 173;
expressionEndOffset = 191;
line = 12;
message = "Result of expression 'Titanium.Media' [undefined] is not an object.";
name = TypeError;
sourceId = 238167336;
sourceURL = "file://localhost/Users/adrian/Documents/Titanium%20Studio%20Workspace/La%20Pause/Resources/movieWindow.js";
}
Can one tell me where am I going wrong?
Thank you for your valuable answers, but Project->Clean solved my issue!
When you use a new object like Titanium.Media that you never used before, a clean is often required because Titanium builds a custom light version in the target folder.
If you don't clean, it will search in vain the widget in this folder.