Tuesday, March 31, 2015

I decided to create a graphical user interface for our framework because it may ease our script execution job and also enhance the functionality of the test framework by having the ability to run with any input test case file without the need of editing the XML everytime. A graphical interface in our case evolved with the use of selecting the input TC file. Later I added the feature to show the results of test execution as well as duration taken for the test operation. A test framework if exported to a runnable JAR file, it is easier for any user to execute this.
In this post I will give you a brief idea how to install Window Builder Pro plugin in Eclipse. After the installation of WindowBuilder Pro, you can develop UI by drag and drop elements required.

Download WindowBuilder Pro plugin:
You may download from the below link

Click here



Select the link as shown in above screenshot.

How to install WindowBuilder plugin in Eclipse:

First open Eclipse, click Help->Install new Software



Click Add, select the Archive from the saved location





Select all options and finish



If you are done with the installation, we shall start the development of user interface.

Create a new Java class, Expand Window builder, then expand Swing Designer and select Jframe, Click Next, Finish.







Now the Jframe class is created, it will have two portions, Source and Design.

Click Design to add UI components. Just for sample, drag a JTextfield and Jbutton from Palette chooser to the frame on right.



Select Jbutton, Right click and add an Action eventhandler- Actionperformed






Click on Action peformed. Now switch to source part. Add below code inside the action performed:

textField.setText(“Button pressed”);


Now save(Ctrl+S) and run as Java application, you will see the UI opening up. Click on the button, observe











Hope these steps will help you to design one yourself.....

Sunday, March 29, 2015

If you are looking for a solution to automate files drag and drop and Adobe flash content in Selenium, you are in the right page. In this post I will familiarize you about Sikuli. Sikuli automates anything you see on the screen. It uses image recognition to identify and control GUI components. It is useful when there is no easy access to a GUI's internal or source code.


About Sikuli:
Sikuli is an open-source research project originally started at the User Interface Design Group at MIT.

How to setup sikuli in Selenium

It's quite simple. You have to download Sikuli_setup.jar from Sikuli project page. Here is the link to download Sikuli. Click here.





Once the download is finished, copy the jar file to any folder. Click the sikuli-setup.jar to start the setup process. Once you double click the JAR file, you will observe two extra files been created, a batch file and a log file. 



Click the prompt shown for your information and proceed. Next, you will observe a screen with few options. To automate using Sikuli along with Selenium in Eclipse, you need to select Options 1,4,6 only.




 Click okay to proceed. Once the setup is finished, you will get two JAR files sikuli-ide.jar and sikuli-java.jar. One is the Sikuli IDE and other is library file you need to add in eclipse project library to write sikuli scripts in eclipse.

Setup environmental variables for Sikuli

You need to add a system environmental variable %SIKULI_HOME%




Since all setup is finished, we may proceed with scripting. Hope you have added Sikuli library file in Eclipse. Now create a java class for Sikuli and write script as below. You may call this method whenever required.

Code for flash automation:
package newpackage;

import org.sikuli.script.FindFailed;
import org.sikuli.script.Screen;


public class Photocapture{
public static Screen screen;
public static void saveimage() throws FindFailed, InterruptedException{
screen =new Screen();
Thread.sleep(1000);
screen.click("images/allowcamera.png");
Thread.sleep(5000);
screen.click("images/captureimage.png");
Thread.sleep(2000);
screen.click("images/saveimage.png");
Thread.sleep(2000);
}

}

Like this you may use functions for drag and drop files. All you need is the image of source file and image of target.

In order to drag and drop files, use below statement:

screen.dragDrop("source image path", "target image path");

Always remember Sikuli automation will work when the given image is visible in your screen.

 I would like to thank Sikuli project team for their wonderful work :)

Friday, March 27, 2015

There are two methods out of which first one is preferrable:

Method1:

First you have to get the Select element of the dropdown using below statement
Select dropdown = new Select(
new WebDriverWait(driver, 10).until(ExpectedConditions
.presenceOfElementLocated(By.id("elementid"))));


Then using the Select element you get, you can select the dropdown value by using any of the below statement


dropdown.selectByVisibleText("dropdownvalue");
or
dropdown.selectByIndex(0);
or
dropdown.selectByValue("value");


Method2:

List<WebElement> dd= new ArrayList<WebElement>();
dd = new WebDriverWait(driver, 10).until(ExpectedConditions.presenceOfElementLocated(By.id("elementid")))
.findElements(By.tagName("option"));
for (WebElement option : dd) {
//System.out.println(option.getText());
if (outname.equalsIgnoreCase(option.getText())){
option.click();
break;
}
}

Method 1 is more preferrable in Selenium webdriver. However in PhantomJS I found first method is not working. So try method 2 in such cases.

Wednesday, March 25, 2015

There are mainly two types:  Implicit Waits and Explicit waits

Implicit Wait

Implicit wait tells webdriver to poll for a specific amount of time. Once the time is over, WebDriver object will expire.

 Explicit Wait

Explicit wait tells the WebDriver to wait for certain conditions to occur before proceeding. By using WebDriverWait in combination with ExpectedCondition we can accomplish this.

Explicit wait are of two types:  WebDriver Wait and Fluent wait

WebDriver Wait:

Check below code:


Webelement element=null;
try{
element= new WebDriverWait(driver, 10).until( ExpectedConditions.presenceOfElementLocated(By.id(“elementid”))))
element.click();
}
catch (TimeoutException toe){

}

Once the wait is over and still the element is not found, it will throw a Timeout exception. So try to code inside a try/catch statement.


Fluentwait:

Webdriver wait is an extension of Fluentwait. In Fluentwait it also specifies the pollingtime to check for the condition of element ignoring nosuchElement exceptions.

Check below code:
 
try {
        new FluentWait<WebDriver>(driver).withTimeout(3, SECONDS)
            .pollingEvery(100, MILLISECONDS)
            .ignoring(NoSuchElementException.class)
            .until(new Function<WebDriver, Boolean>() {
              public Boolean apply(WebDriver d) {
                WebElement link = d.findElement(By.linkText(linkText));
                return link.isDisplayed();
              }
            });
        } catch(TimeoutException te) {
          assertFalse(String.format("Timeout waiting for link: '%s'", linkText), true);
        }

Thursday, March 19, 2015




 
Selenium webdriver helps in automating web applications by loading the application in browsers. But during the execution it's not possible to minimize/hide the browser even if the user intend to. There are tweaks such as setting the window size like driver.manage().window().setPosition(new Point(-2000, 0));. But a question arises, if browser is invoked in minimized mode, whats the need of invoking it. Why don't we run it in background? Such a question arised in mind too. A good answer for this is PhantomJS. PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.
Like many other packages, PhantomJS supports Selenium webdriver too. You need to download the executable file from PhantomJS webpage, store the .exe file in class resource path. This can be used to load webdriver.

See below code:

public void navigation() {
DesiredCapabilities caps = new DesiredCapabilities();

String phantompath= loadPhantomJS();

caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, phantompath);

try{

driver = new PhantomJSDriver(caps);

}

catch(IllegalStateException ie){

if(driver!=null){

driver.quit();

}

}

driver.manage().window().setSize(new Dimension(1124,1024));
driver.get(obj.getProperty("url"));

    }


private static String loadPhantomJS() {
        String phantomJs = "phantomjs.exe";
        String dir1 = System.getProperty("user.dir");;
        try {
            InputStream in = Exitfunctions.class.getResourceAsStream("/phantom/" + phantomJs);
            File fileOut = new File(dir1 +"/"+ phantomJs);
            OutputStream out = FileUtils.openOutputStream(fileOut);
            IOUtils.copy(in, out);
            in.close();
            out.close();
            return fileOut.getAbsolutePath();
        } catch (Exception e) {
            return "";
        }
    } 



PhantomJS need to quit once the execution is done or any exception occurs. Unless, PhantomJS service will be running in background which may cause exception while next execution of your program. You may use driver.quit(); itself.

Link to download PhantomJS: http://phantomjs.org/

>

Selenium useful links

Powered by Blogger.

Featured Post

Benefits of having a user Interface for a Selenium Automation Suite/Regression Suite

Once you are able to work independently on Selenium scripting, another important task is how to ease your automation test execution. There a...

Video

Popular Posts

Our Facebook Page