Saturday 20 October 2012

Selenium Web Driver to test websites

In this post I will write how to use selenium web driver to test the websites.Selenium  web driver allows us to drive the browser from the code supporting many languages like java , python etc.While it takes a little setup for chrome and ie,it works with firefox without any setup.I am using java to drive firefox browser.

The main goal of this post is to share code on how to design the test suite to make the code most reusable and also to make the testing reliable irrespective of user's connection speed.On to the design now :

The basic approach to design a test suite should be to create methods which automate elementary tasks and then to use these methods from the testing class and assert the results.This way the code developed for automating elementary tasks can be used by any potential testing client.For example : for website testing, tasks like logging in,logout,using various features of site like create post in blogger etc. can be put in a common class.

The test suite I developed is to test a salesforce app , so I will provide methods which helps doing login,selecting apps from the top right,selecting tabs,asserting page title etc.

Also I have overridden the selenium web driver's default methods like findElement() and some others to wait them for given time for page to load before throwing element not found kind of exceptions resulting from incomplete page load.

Please set the variable TIMEOUT according to connection speed.The logic behind waiting till timeout is the recursive call of the methods each after 1 sec.The methods exit after TIMEOUT if the desired elements are not found.

20 comments:

  1. Good content presentation! Thanks for putting the efforts on gathering useful content and sharing here. You can find more Selenium testing related question and answers in the below forum.

    Selenium testing question and answers

    ReplyDelete
  2. Excellent Post. Also visit http://whiteboxqa.com/#selenium.php.

    ReplyDelete
  3. Fantastic article ! You havemade some very astute statements and I appreciate the the effort you have put into your

    writing. Its clear that you know what you are writing about. I am excited to read more of your sites content.

    Microsoft dynamics training

    ReplyDelete
  4. Hi,
    Can you tell me how we can take the user input and execute it by the help of driver.get(""); I tried but it is able to open the browser but not hitting the URL.

    public class Text
    {


    static String str;
    static class ButtonFrame extends JFrame
    {
    @SuppressWarnings("LeakingThisInConstructor")
    public ButtonFrame()
    {


    setTitle("Mobile Portal");
    setSize(900,900);
    ButtonPanel panel = new ButtonPanel();
    panel.add(new JLabel("Enter your URL"));
    loanAmt = new JTextField(40);
    panel.add(loanAmt);

    add(panel,BorderLayout.CENTER);
    }


    public JTextField loanAmt;


    class ButtonPanel extends JPanel implements ActionListener
    {
    private Component frame;
    public ButtonPanel()
    {

    final JButton b2 = new JButton("Submit");
    add(b2, BorderLayout.SOUTH);
    b2.setActionCommand("Submit");
    b2.addActionListener(new ActionListener(){
    public void actionPerformed(ActionEvent e)
    {
    ButtonFrame bf = new ButtonFrame();
    if("Submit".equals(e.getActionCommand()))
    {
    visit();

    // JOptionPane.showMessageDialog(frame, bf.loanAmt.getText());
    }
    }

    });

    }

    @Override
    public void actionPerformed(ActionEvent ae) {
    throw new UnsupportedOperationException("Not supported yet.");
    }


    }
    public static void main(String[] args)
    {

    ButtonFrame frame = new ButtonFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    System.out.println("After driver get");

    // driver.get(str);

    }
    }
    public static void visit() {
    ButtonFrame frame = new ButtonFrame();
    WebDriver driver=new FirefoxDriver();
    driver.get("www.google.com");
    str=""+frame.loanAmt.getText();
    System.out.println("Str----->"+str);

    }

    }


    Please can you help me.

    ReplyDelete
    Replies
    1. u can try http:\\www.google.com instead 0f www.google.com

      Delete
  5. It was nice article it was very useful for me as well as useful foronline Selenium training learners. thanks for providing this valuable information.

    ReplyDelete
  6. Thanks for this good information and it is useful for selenium Learners.123trainings provides best SELENIUM online training.

    ReplyDelete
  7. it is useful for selenium Learners.123trainings provides best SELENIUM online training.

    ReplyDelete
  8. The best content on selenium is over here....
    Thanks Vishesh Singhal for sharing it....

    ReplyDelete
  9. Daliy Deals and Coupons
    DailyDealsnCoupons offer the best discount codes, promotion codes, latest coupons and hot deals for various stores. You can find great discount deals on your online purchases at hundreds of your hot favorite online stores, brands, categories or products.

    If you're looking for Spam free coupon code website, visit dailydealsncoupons.com

    ReplyDelete
  10. Hi Vishesh. Code is excellent. But i am Beginner to selenium, so i am not understanding how to run this code. Please help me how to run this code.

    ReplyDelete
  11. SAVING TESTNG RTESULTS WITHOUT OVERWRITING PREVIOUS RESULTS
    Am using selenium webdriver (ANT+TestNG+Java).How do I save results of TestNG with time-stamp and build no.(testng-results-Build no:DD-MM-YYYY_HH-MM-SS.xml),without overwriting previous results. Could anyone share the build.xml code?

    ReplyDelete
  12. how to capture links like # and javascript void (0) in selenium webdriver

    ReplyDelete
  13. I am trying to automate actions and unable to select an element due to its dynamic nature.
    I am running Selenium web driver on ruby and am trying to value that is not present when I try to view it in page source.




    <_div class="processBarElement noSelected">
    <_div class="whiteBeforeProcessBarTitles">Initial Selection
    <_div class="endOfElementOfProcessBar">




    I am trying to select value "Initial Selection" from above.


    Could you pls help out VIshesh?

    Thanks,
    Abhishek

    ReplyDelete
  14. Your information about selenium is really interesting. Also I want to know the latest new techniques which are implemented in Selenium. Please update it in your website.
    Selenium training Chennai

    ReplyDelete
  15. Hi Vishesh,

    I need ur help to test sort functionality.below are the listed steps i want to do, however i am not able to proceed further failing in the first step itself.


    1. Read data from column & store it in an array
    2. Sort this array (with same pattern as application sorts)
    3. Click operation on AUT
    4. Read value from column & save it in 2nd array
    5. compare value of both array

    Regards
    T.Sunil

    ReplyDelete
  16. This is a real practical analysis of selenium training. It was really helpful for me. Palash

    ReplyDelete