Hey there! any question in your mind? Ask It Now!.

Popular Categories





How to handle flex in selenium web driver?

+10 votes
8,027 views
asked in Software Testing by tester (1,550 points)
retagged by Bhartesh
Hello guys i am trying to handle flex in selenium but not getting any solution please tell me how can i handel flex.

Thank you!

2 Answers

+8 votes
answered by seleniumGuru Expert (4,570 points)
selected by Bhartesh
 
Best answer

To automate flex using selenium you have to follow these steps :

  • You have to download selenium flex API .
  • You will get a SeleniumFlexAPI.swc file here.

    a.  Now add this SeleniumFlexAPI.swc file into your libs folder (This folder would be in your project or check your repo and search libs folder).

    b. After adding that file in libs folder now you will have to compile your code so that SeleniumFlexAPI.swc file      can include in code , for compiling you need to add lib path in flex compiler like this :

                                                       -include-libraries "libs\SeleniumFlexAPI.swc"

Now Compile the code.

Next point 3 is in the first comment please follow that after executing these above steps.

commented by Bhartesh (2,610 points)
Hi Eddy,  First of all, this SWF/SWC file should be added to you flex project which needs to be automated . Make sure you are adding it to correct project.

And for how to compile, pls refer this link : http://help.adobe.com/en_US/flashbuilder/using/WSe4e4b720da9dedb5-1a92eab212e75b9d8b2-7ffe.html
commented by Kushal
Hi I'm getting following error, here Selben is my ID name:

Exception in thread "main" org.openqa.selenium.WebDriverException: document.selben.doFlexClick is not a function
commented by kushal
edited
^^can anybody help me with the above?
0 votes
answered by Bhartesh (2,610 points)

Try this code : 

  public String click(final String objectId, final String optionalButtonLabel) {    

    return call("doFlexClick", objectId, optionalButtonLabel);

  }

  public String click(final String objectId) {

     return click(objectId, "");

  }  

import org.openqa.selenium.JavascriptExecutor;

import org.openqa.selenium.WebDriver;

publicclass FlexWebDriver {  privatefinal WebDriver webDriver;privatefinal String flashObjectId;  public FlexWebDriver(final WebDriver webDriver, final String flashObjectId) {    this.webDriver = webDriver;

    this.flashObjectId = flashObjectId;

  }

  public String click(final String objectId, final String optionalButtonLabel) {    return call("doFlexClick", objectId, optionalButtonLabel);

  }

  public String click(final String objectId) {

     return click(objectId, "");

  }

  private String call(final String functionName, final String... args) {final Object result =

      ((JavascriptExecutor)webDriver).executeScript(

         makeJsFunction(functionName, args),

         new Object[0]);

     return result != null ? result.toString() : null;

  }

  private String makeJsFunction(final String functionName, final String... args) {

     final StringBuffer functionArgs = new StringBuffer();

    if (args.length > 0) {

      for (int i = 0; i < args.length; i++) {

        if (i > 0) {

          functionArgs.append(",");

      }

        functionArgs.append(String.format("'%1$s'", args[i]));

     }

    }

    return String.format(

      "return document.%1$s.%2$s(%3$s);",

      flashObjectId,

      functionName,

      functionArgs);

  }

}

 

Now the example of using clicking an element :

final FirefoxDriver driver = new FirefoxDriver();

final FlexWebDriver flexDriver = new FlexWebDriver(driver, “my-application”);

driver.get(“http://localhost:8080/application”);

 Assert.assertEquals(“true”, flexDriver.click(“aButton”));

commented by anonymous
call("doFlexClick", objectId, optionalButtonLabel);

what is doFlexClick???

Assert.assertEquals(“true”, flexDriver.click(“aButton”));

what is aButton????
commented by Bhartesh (2,610 points)
Sharing sample code for the same, check this maybe you can get the idea about doFlexClick.

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class FlexWebDriver {    
    private final WebDriver webDriver;
    private final String flashObjectId;    
    public FlexWebDriver(final WebDriver webDriver, final String flashObjectId) {       
        this.webDriver = webDriver;
        this.flashObjectId = flashObjectId;
    }

    
     public String doFlexClickMenuBarUIComponent(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexClickMenuBarUIComponent", objectId, optionalButtonLabel);
   }

    public String doFlexClickMenuBarUIComponent(final String objectId) {
         return doFlexClickMenuBarUIComponent(objectId, "");
    }
    public String click(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexClick", objectId, optionalButtonLabel);
   }

    public String click(final String objectId) {
         return click(objectId, "");
    }
    public String ClickDataGridItem(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexClickDataGridItem", objectId, optionalButtonLabel);
   }

    public String ClickDataGridItem(final String objectId) {
         return click(objectId, "");
    }
    
    public String FlexClickDataGridUIComponent(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexClickDataGridUIComponent", objectId, optionalButtonLabel);
   }

    public String FlexClickDataGridUIComponent(final String objectId) {
         return FlexClickDataGridUIComponent(objectId, "");
    }
    
    public String FlexSelectComboByLabel(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexSelectComboByLabel", objectId, optionalButtonLabel);
   }

    public String doFlexSelectComboByLabel(final String objectId) {
         return click(objectId, "");
    }
    public String FlexSelect(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexSelect", objectId, optionalButtonLabel);
   }

    public String FlexSelect(final String objectId) {
         return FlexSelect(objectId, "");
    }
    public String FlexRefreshIDToolTips(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexRefreshIDToolTips", objectId, optionalButtonLabel);
   }

    public String FlexRefreshIDToolTips(final String objectId) {
         return FlexRefreshIDToolTips(objectId, "");
    }
    public String FlexDoubleClick(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexDoubleClick", objectId, optionalButtonLabel);
   }

    public String FlexDoubleClick(final String objectId) {
         return FlexDoubleClick(objectId, "");
    }
    public String FlexSetFocus(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexSetFocus", objectId, optionalButtonLabel);
   }

    public String FlexSetFocus(final String objectId) {
         return FlexSetFocus(objectId, "");
    }
    public String FlexMouseMove(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexMouseMove", objectId, optionalButtonLabel);
   }

    public String FlexMouseMove(final String objectId) {
         return FlexMouseMove(objectId, "");
    }
    public String FlexType(final String objectId, final String optionalButtonLabel) {       
        return call("doFlexType", objectId, optionalButtonLabel);
   }

    public String FlexType(final String objectId) {
         return FlexType(objectId, "");
    }
    
    public String getFlexDataFieldLabelForGridRow(
            String dataGridId, String field, int row) {
        return call("getFlexDataGridFieldLabelForGridRow", dataGridId, field, Integer.toString(row));
    }
    public int getFlexDataGridRowCount(String dataGridId ) {
        return Integer.parseInt(call("getFlexDataGridRowCount", dataGridId));
    }
    public int getSelectionIndex(String selectionFieldId) {
        return Integer.parseInt(call("getFlexSelectionIndex", selectionFieldId, ""));
    }

//... Omitted for clarity ...

    private String call(final String functionName, final String... args) {final Object result =
            ((JavascriptExecutor)webDriver).executeScript(
                 makeJsFunction(functionName, args),
                 new Object[0]);

         return result != null ? result.toString() : null;
    }

    private String makeJsFunction(final String functionName, final String... args) {
         final StringBuffer functionArgs = new StringBuffer();

        if (args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                if (i > 0) {
                    functionArgs.append(",");
            }
                functionArgs.append(String.format("'%1$s'", args[i]));
         }
        }
        return String.format(
            "return document.%1$s.%2$s(%3$s);",
            flashObjectId,
            functionName,
            functionArgs);
    }


}

Related Questions

0 votes
0 answers 97 views
+1 vote
1 answer 477 views

Not a Member yet?

Ask to Folks Login

My Account
206 Folks are online
0 members and 206 guest online
Your feedback is highly appreciated