Archive

Archive for May, 2013

Using hashtable for include many values in an array – Webdriver (Java)

May 26, 2013 1 comment

Hello again,

I had this new thing that I have learned and use almost every days.

You can use this when you need return many values of some table.This code will return a list of things with a column and theirs line. So, here go the code:

Hashtable valores = new Hashtable<String, String>();

int qtdLinesTblSimAcu = 0;

if(tableSimPresent)

{

String[] cTabelSim = new String[10];

cTabelSim[1] = “ID”;

cTabelSim[2] = “Buyer.Firm”;

cTabelSim[3] = “Buyer.Account”;

cTabelSim[4] = “Buyer.Qty”;

cTabelSim[5] = “Seller.Firm”;

cTabelSim[6] = “Seller.Account”;

cTabelSim[7] = “Seller.Qty”;

cTabelSim[8] = “Price”;

cTabelSim[9] = “Traded.Qty”;

//LET CATCH ALL THE LINES AND PUT IN HASHTABLE

int qtdLinhasTblSim = (Integer) selenium.getXpathCount(//html/tbody/table/tr“)); // CHANGE FOR THE CORRECT XPATH ONLY UNTIL THE TR TAG.

qtdLinesTblSimAcu += qtdLinesTblSim;

if(qtdLinesTblSim > 0)

{

//NAVIGATE FOR LINES AND COLUMNS

for(int l = 2; l <= qtdLinhasTblSim; l++)

{

//NAVIGATE FOR COLUMNS

for(int c = 1 ;c < 10;c++) //CHANGE FOR THE NUMBER OF COLUMNS

{

String val  = selenium.getText(String.format(“xpath=//html/tbody/tr[%s]/td[%s]“),l,c)); /*CHANGE FOR THE XPATH CORRECT, IN THE %s YOU WILL SUBSTITUTE RESPECTIVELY FOR VALUE OF L AND C, LIKE LINE AND COLUMN. */

values.put(cTabelSim[c] + “:” + ((l + qtdLinesTblSimAcu) -1),val);

}

}

}

/* THIS IS THE END OF THE RETURN, IF YOU HAVE CATCH VALUES OF ANOTHER TABLE, PUT THE FOR FOR CATCH THE VALUES OF THIS TABLE, AND THE CODE BELOW IN THE END OF THIS LAST FOR   */

String[][] returnE = new String[values.size()][2];

Enumeration e = values.keys();

int index = -1;

while(e.hasMoreElements())

{

index++;

String k = (String) e.nextElement();

returnE[index][0] = k;

returnE[index][1] = values.get(k).toString();

}

return returnE ;

}

The return will be something like this:

columnA:1 valueA1

columnA:2 valueA2

columnB:1 valueB1

columnC:1 valueC1

columnC:2 valueC2

columnC:3 valueC3

Bye bye, guys !!

Desired capabilities Webdriver – Ignore security in browser

Hello again, I had some codes to show you !

Like this one, when you have some problem with the certificate page, or proxy, or another security problem, you can put this code for initialise the webdriver. Before you code anything, you have to start the webdriver and config this one, you can use this config for start the webdriver with the config of all security is off.

In this example I use the IE 9 and platform windows like the default browser:

DesiredCapabilities capabilities = new DesiredCapabilities(“Internet Explorer”, “9”, Platform.WINDOWS);
capabilities.setJavascriptEnabled(true);
capabilities.setCapability(“ignoreProtectedModeSettings”, true);
capabilities.setCapability(“ensureCleanSession”, true);
capabilities.setCapability(InternetExplorerDriver.INTRODUCE_FLAKINESS_BY_IGNORING_SECURITY_DOMAINS, true);
WebDriver driver = new InternetExplorerDriver(capabilities);

 

Bye bye !!

List of how mount expressions of xpath

Hey guys !!

I’m found a interest and very usefull thing for us mount expressions of xpath.

 

Look this site, they have all single expressions and a short explanation for each one.

https://opencast.jira.com/svn/MH/trunk/modules/matterhorn-runtime-info-ui/src/main/resources/uitests/tests/TestXPathLocators.html

I hope this helps you, the same way helps me !

Good bye 🙂

One solution for: Error in execution of Webdriver

Good night,

When you have errors in a execution of webdriver, and for some reason appears a message told that selenium is closed and your selenium doesn’t really closed.

Put this code for initialize the execution:

String Servidor = “http://127.0.0.1:4444/wd/hub“;
DesiredCapabilities capabilities = DesiredCapabilities.internetExplorer();
WebDriver driver = new RemoteWebDriver(new URL(Servidor),capabilities);

This way you will execute the test in your local machine and force this.

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 🙂

%d bloggers like this: