Archive

Archive for March, 2013

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 !

Advertisements

Mouse up of a WebElement (Selenium and Java)

Move the mouse up of a controller/WebElement is simple, is just put this line of code:

WebElement valoratual = driver.findElement(By.className(“product-description-catalog-item”));

Robot robot = new Robot();
robot.mouseMove(valoratual.getLocation().getX(), valoratual.getLocation().getY());

 

Yes, you have to catch the X and Y in the page and the robot will put the mouse over thispoint.

 

Bye bye 🙂

Do you need scroll the page ? (Selenium and javascript)

If you need scroll the page for example take a screenshot in a browser different of Firefox (Yes, the selenium have a problem to take a screenshot of the entire page when you are in a different browser like, IE)

So we have to create a function that take a screenshot of parts of the page, and this function helps you to scroll the page.

First you have to import the libraries:

import org.openqa.selenium.JavascriptExecutor;

 

After the code:

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript(“window.scrollTo(0,Math.max(document.documentElement.scrollHeight,”
+ “document.body.scrollHeight,document.documentElement.clientHeight));”);

 

After this, the driver will scroll until the end of the current page.

I hope that I help you 🙂

Mark element in the page with red border (Javascript with Selenium)

March 14, 2013 3 comments

Hello again, now I will give a tip of Selenium, for mark the element that you want and before this, you can take a screenshot for evidence.

First you have to import this libraries:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;

 

After this, you have to call the javascript executor and give the id or other thing for find the element:

JavascriptExecutor js = (JavascriptExecutor) driver; // change the name of your WebDriver
WebElement element = driver.findElement(By.id(“element“)); // change the localizator 

js.executeScript(“arguments[0].style.border=’3px solid red'”, element); // change the name of your variable

 

This is very usefull for mark the element in the page.

So, just this ! Bye bye 🙂

What a constructor do ?

Hum.. I always forget the function of a constructor because I don’t have the custom to use.

For this reason, I will put here some tips:

A constructor  is a method with instructions that will execute ALWAYS  when will instantiated a object of this class.

Like this:

  • Class MyClass.java
 public class MyClass {

public MyClass() { //this is the constructor method
System.out.println(“Hello World !”);
}  }

In the constructor method will be wrrte “Hello World !”

So… when you call this class, the constructor will be execute.

 

  • Class Test.java
 public class Test {

public static void main(String args[]) {
MyClass Obj1 = new MyClass();
}
}

It’s simple no ? It is just a method that will be execute before others methods when you call this class.
Bye bye !

The best small caps in March

Sao Paulo – With changes in index of small caps of Bovespa, the compiled of stocks more recommended made by InfoMoney shows a different result  of last month. In the third month of 2013, the America Latina Logistica (ALLL3), wich not was in the february index, is in the first position with six votes between 25 of analysed portfolio.

The second place was with Anhanguera (AEDU3), wich was recommended in five of 25 analysed portfolio. The third place was divided between the companies Even (EVEN3) and PDG Realty (PDGR3), wich were cited three times each.

 

Others recommendations:

Alpargatas (ALPA4), Eztec (EZTC3), Grendene (GRND3), Iguatemi (IGTA3), Kroton (KROT3), Marfrig (MRFG3), Iochp-Maxion (MYPK3), Marcopolo (POMO4), Randon (RAPT4), São Martinho (SMTO3), V-Agro (VAGR3) e Valid (VLID3).

 

With one vote each, close the list of recommendations the papers of  Aliansce (ALSC3), Lojas Marisa (AMAR3), Banrisul (BRSR6), Equatorial (EQTL3), Fleury (FLRY3), Gafisa (GFSA3), Helbor (HBOR3), Metal Leve (LEVE3), Le Lis Blanc (LLIS3), Magnesita (MAGG3), Mils (MILS3), MRV (MRVE3), Odontoprev (ODPV3), Paranapanema (PMAM3), Qualicorp (QUAL3), Rossi Residencial (RSID3), SierraBrasil (SSBR3) e Santos BRP (STBP11).

 

Font: http://www.infomoney.com.br/onde-investir/acoes/noticia/2697172/small-caps-mais-recomendadas-para-marco-por-corretoras

How can I call the drivers(browsers) in Selenium with java ?

It’s simple !

you have to call the import > import org.openqa.selenium.WebDriver; and others about the browsers, like: import org.openqa.selenium.firefox.FirefoxDriver; (calls Firefox)

After this, in the class Main, declare driver:

WebDriver driver;

and Quit:

Quit quit = new Quit();

So… you have now choose what is the browser and calls the profile and set the configs, like proxy (for this reason you have to create a profile)

public class Main {

public static void main(String[] args) throws Exception {

FirefoxProfile profile = new FirefoxProfile();   // create a profile of Firefox
profile.setPreference(“network.proxy.type”, 0);   // Don’t use proxy
driver = new FirefoxDriver(profile);       // calls the browser with the configs of the profile
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);     // config the timeouts for load a page
driver.get(site);     // change site for the url that your driver will open.

quit.tearDown(driver);     // quit the driver.

}

}

You have to create the class that close the driver in class Main.

public class Main {

public static class Quit {

public void tearDown(WebDriver driver) {
driver.close();
driver.quit();
}

}

and finish !

This is the first step for create a automation with selenium. Create a driver and open the browser !

Good studies for all, bye !

%d bloggers like this: