Other ways to turn on or off the caps lock

Turn on/off the Caps lock with Selenium RC and key press and key up:

selenium.keyUpNative(KeyEvent.VK_CAPS_LOCK.toString()); // Turn off the caps Lock, you can use this if ou don’t know if the caps lock is on or off. So you can turn off before you press the key.

selenium.keyPressNative(KeyEvent.VK_CAPS_LOCK.toString()); // Turn on the caps lock

With the Robot:

Robot robo = new Robot();
robo.keyPress(KeyEvent.VK_CAPS_LOCK);

Very easy, and if one way doesn`t work, you can try another 🙂

Bye bye !

Solution for error: No X11 DISPLAY variable was set, but this program performed an operation which requires it.

Solution for the problem with error:

ERROR com.cme.brio.selenium.ScriptException: The Script has thrown exception which is handled properly. The Selenium instance is closed and if any other script requires context of this Selenium instance will also fail. Please see the stack trace in the log and underlying exceptions to know the root cause of this problem.

ERROR Caused by: java.lang.reflect.InvocationTargetException

ERROR Caused by: groovy.lang.MissingMethodException: No signature of method: star.sa.VerifyInstrument.failResults() is applicable for argument types: (java.awt.HeadlessException) values: [java.awt.HeadlessException: No X11 DISPLAY variable was set, but this program performed an operation which requires it.] Possible solutions: findResult(groovy.lang.Closure)

ERROR An error occured while executing the script. Reason: Exception thrown. Please see logs for the complete stack trace.

 

When you get his error, just put above the code with Toolkit this line:

System.setProperty(“java.awt.headless”, “false”);
                        System.out.println(java.awt.GraphicsEnvironment.isHeadless());

Just this will enable the keys in whatever pc.

Bye bye 🙂

Search a attribute that contains a word in Selenium RC

Hello,

Today I started post everything that I have learned in the work.

This time I will post the last thing and most usefully command that I learned.

selenium.getAttribute(“id=Element@style“).contains(“display: inline“)

You need to change the bold words, in the first word, you have to change the locator (id, xpath, class, name, link and others). After this you have to change the name of locator respectivally.

Just note that after the locator we have @style this is the attribute in the element that we will catch. Could be @class, or @title, anything and stay in the final of locator separates with @.

In contains, you can change for the tag you need find, like I want the element, whom style contains a display: inline, could be text=’something’ or value=’something’. Like this, simple doesn’t ?

Bye bye 🙂

Select in list of Elements (WebDriver)

How can I do a select in a list of elements with some attributes in equal. This is simple, like a query in SQL:

First you have to create a enumerable of WebElements:

Enumerable<WebElement> list = from item in bsaNavigator.FindElements(By.TagName(“span“)).ToList()   // the list of elements are span or td, or whatever you want. With Xpath, or id… you will decide .

where item.GetAttribute(“class“).Contains(“rmText“)  &&  item.Text.Equals(“TextElement“.ToString())  //this is the condition that you will make, like every elements of span who have the attribute of the class have contains the text “rmText” and the Text is equals a something you need.

select item; // select the item with the conditions

WebElement Element = list.FirstOrDefault();  // catch the first element of the list with selects.
Element.Click();    // the action or whatever you want do with the element.

Simple and very usefull !!

Bye bye !

Context click with actions (WebDriver)

Hello !!

 

Do you need simulate a context click (right-click) in some element ?

With the lib actions is simple. First you need import the actions of java and after

 

Actions actions = new Actions(driver);     //Instance the Actions

WebElement Element = driver.findElement(By.id(“ID“)); // find the element and put is in a variable

actions.ContextClick(Element).Perform(); // position the mouse over the element and context click it.

 

Just this, in some cases you have put a speed more lower or a wait element os something like that to work well.

 

Bye bye !!

 

Find Elements with different ways

Two different ways for find a element of a list in a page with WebDriver:

To find a element that inside in a span css and contains some text, you can use the lambda and catch the array for this elements: 

> Lambda expression
bsaNavigator.FindElements(By.TagName(“span“)).Where(o => o.Text == “@Text“).ToList()
                                       

Or you can select the element, like SQl expression, remember of change the variables and the type of the source (span, div, table, tr and others). @Text is the variable that you will search in this span elements .

> Linq
                                       
var spans = from p in bsaNavigator.FindElements(By.TagName(“span“)) where p.Text == “@Text” select p;

 

Do you have some doubt ? You can ask me in the comments !

Bye bye 🙂

Regular Expression for value of the money (Java)

Do you have to catch only the value of some value of money ?

Use this simple expression, just change the value R$ for the correct money that you need take out.

String preco = precoatual.getText();

String replace = “”;
Pattern pattern = Pattern.compile(“[^0-9&&[R$]]”);
Matcher matcher = pattern.matcher(preco);
preco = matcher.replaceAll(replace);

And if the value is: R$ 124,28 – The result will are: 124,28

Simple don’t ?

Bye bye !