Selenium: Check if a WebElement has the focus - selenium

I was expecting something like WebElement.isfocus(),... something really simple, but the only approach I found is using the
:focus
pseudo class.
Is this really such an uncommon task as for not find tons of information?
I'm aware of this SO topic, but it's been almost two years from then. Nothing new in more recent versions?
Would you know some workaround?

There is another topic that covers this issue: Test if an element is focused using Selenium Webdriver
Basically the code will be
element.equals(driver.switchTo().activeElement());

for python developers:
def is_element_focus(id):
return self.driver.find_element_by_id(id) == self.driver.switch_to.active_element

Related

How to find text from an image and identify it's coordinated using karate UI and karate Robor

I want to highlight a window and find the text in an image and also get its coordinates. How can we do this through karate UI and karate robot.
This question has not come up before, so it will require some research. I will try to provide some hints below, but this may require someone with knowledge of Java and the Windows API to figure out.
First, refer the docs: https://github.com/karatelabs/karate/tree/master/karate-robot
Behind the scenes, Karate Robot is using the Windows API: https://learn.microsoft.com/en-us/windows/win32/winauto/entry-uiauto-win32
And refer the Element implementation: https://github.com/karatelabs/karate/blob/v1.3.1/karate-robot/src/main/java/com/intuit/karate/robot/win/WinElement.java
If I remember correctly, you should be able to call element.property('') and get the values you want.
Here is the reference for all properties: https://github.com/karatelabs/karate/blob/v1.3.1/karate-robot/src/main/java/com/intuit/karate/robot/win/Property.java
So maybe this will work (I have not tried):
* def e = locate('locator')
* def rect = e.property(30001)
Which I think will get you this: https://learn.microsoft.com/en-us/windows/win32/api/uiautomationcore/ns-uiautomationcore-uiarect
I am not sure how to get the values out, maybe rect.left etc. will work, or it may be a JSON. It will be great if you or your team can contribute if needed to the code and documentation.

Karate driver synchronization challeges

Karate driven execution fails once in a while for certain elements where I use xpath. (Every three runs; one or two times it works.)
I tried the following code,
waitUntil("document.readyState == 'complete'")
click("xpath") This line fails once in a while.
The xpath is consistently working on my existing Java Selenium code.
Can another synchronization method be applied?
This will solve the problem:
* waitFor('xpath').click()
Please read this part of the documentation carefully: https://github.com/intuit/karate/tree/develop/karate-core#wait-api
Also I have found "document.readyState == 'complete'" to be unreliable. Instead try waitForUrl('partial/url').

Two different syntax structure for Selenium

I'm using using AutoHotkey to drive SeleniumBasic v2.0.9.0
I'm new to Selenium and have been looking at a lot of different pages discussing how to get/set elements on a webpage. I've noticed there seems to be (at least )two different types of format for syntax.
Here are two examples:
1. driver.findElementByID("search_form_input_homepage").SendKeys("hello")
2. driver.findElement(By.id("search_form_input_homepage")).SendKeys("hello")
In my case the first one works but the second throws an error saying No such interface supported. I'm just curious of the origin of the second structure. Is it from Selenium 3?
Here is the Answer to your Question:
driver.findElementByID("search_form_input_homepage").SendKeys("hello") : Is in use through the VBA module maintained by #FlorentB.
driver.findElement(By.id("search_form_input_homepage")).SendKeys("hello") : Is in use through the Java bindings of Selenium.
Let me know if this Answers your Question.

Can HTMLUnit package be used with PhantomJsDriver.java

I am using "org.openqa.selenium.phantomjs.PhantomJsDriver" Java class.
At times, I need to identify whether a given WebElement is a particular type of web element; eg: Is this webelement a input type element or not.
This does not seem to come with PhantomJsDriver package.
"com.gargoylesoftware.htmlunit." package seems to have useful wrappers on top of web elements. I can write code like "element instanceof HtmlInput".
Question here is
- Can I really HTMLunit package with phantomjsdriver ? Am I using two libraries which are not supposed to be used with one-another ?
No. Unfortunately, you can't do this the way you're doing it. PhantomJsDriver is backed by WebKit, while HtmlUnitDriver is backed by HtmlUnit which has its own browser core. Selenium is able to wrap both these (and some more) under one hood, but we can't use them interchangeably.
There are, however, different ways of doing what you're trying to do, the best probably being using Selenium's own methods getTagName() and getAttribute() if needed.
If you ran getTagName() on your input element, it would gladly return "input".

Learning GEB and Spock

I am a manual tester trying to learn GEB and Spock. To learn these do I have to have prior knowledge of java or groovy? I have been reading the book of GEB, What are the prerequisites, books or learning resources? Please help. Thanks.
I tried compiling some essentials and some 'good-to-haves' that I found very helpful when I picked up Geb.
Some Groovy Magic. Most of all that you need to learn Groovy is covered in this manual but for obvious reasons if you get obsessed with the language you might want to consider Groovy in Action. While Java is not needed to pick up Groovy, If you are from a Java (except for closures) or even a Python background, you could probably skim through the tutorial for 15 minutes and you are there already).
A little Selenium. The more, the better but fear not, this single page tells you all that you need to know about the Selenium Webdriver that you would generally use. But to stress, the more the better.
jQuery selectors (everybody says that it is easy but frankly, I refer to the manual at least twice per hour. I am dumb, so…). If you are new to jQuery, you would want to start from basic selectors and click on the left navigation menu for more. Please note that not all jQuery selectors are applicable for Geb but the selectors section of Geb tutorial wasn't very exhaustive and engaging.
At the end of my testcases, I need to generate a fanciful report which stretches across multiple testcases and I had dependencies among testcases. So, I went for TestNG instead of Spock. Frankly, I didn't give Spock a lot of chance.
PageObjects is actually not a prerequisite for Geb but PageObjects are so awesome that you never wanted to think about Geb outside of it. PageObjects is a cute little pattern which says that you wrap the structure of your HTML page into an Object so that the actual test does not have to deal with it. Hah. Got you. Let me put that in plain English.
Say, you have a registration form with input textbox which has an id of "nametext". How would you get the handle of the textbox? In DOM terms, in javascript, you would just do a
document.getElementById("nametext")
In Selenium, you would do a very similar thing
driver.findElement(By.id("nametext"))
So, if you would want to populate Jason in your text box in Selenium, you would do a
driver.findElement(By.id("nametext")).sendKeys("Jason");
If you do that for all your input fields, very soon your testcases become ugly and hateful. Instead of that, in OO terms, you encapsulate. You create a new class, say RegistrationPage and wrap your findElement and sendKeys as in :
public class RegistrationPage{
…
public RegistrationPage fillRegistrationForm(String name, String email){
driver.findElement(By.id("nametext")).sendKeys(name);
driver.findElement(By.id("emailtext")).sendKeys(email);
}
}
and from your testcase, you would say
RegistrationPage regPage=new RegistrationPage();
regPage.fillRegistrationForm("Jason","jason#bourne.com");
(Even better idea is to wrap your input values into a class and pass it to the fillRegistrationForm)
In fact, Geb leverages PageObjects in a much better way - jQuery selectors to the rescue
class InputFormPage extends Page{
…
static content={
name {$("input", id:"entry_0")}
emailAddress {$("input", id:"entry_1")}
}
}
and in your testcase, you would just say
name.value ("Jason")
emailAddress.value ("jason#bourne.com")