Wednesday, 19 February 2014

how to select a checkbox using webdriver


1.If all checkbox's having Inside one <td> like As
<td>
<input type="checkbox" name="module" value="xxx">
xxx
<br>
<input type="checkbox" name="module" value="yyy">
yyy
<br>
<input type="checkbox" name="module" value="zzz">
zzz
<br>

</td>


 webdriver selecting checkbox script is:

driver.findElement(By.xpath("//input[@name='module' and @value='zzz']")).click(); 
driver.findElement(By.xpath("//input[@name='module' and @value='yyy']")).click();
driver.findElement(By.xpath("//input[@name='module' and @value='xxx']")).click();



2.if all checkbox having one <td> with <table> structure like as

<td width="50%" align="left">
<table border="0" align="left">
<tbody>
<tr>
<td align="left">
<input type="checkbox" name="modules" value="3" style="width:10px">
</td>
<td align="left">zzz</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="modules" value="2" style="width:10px">
</td>
<td align="left">yyy</td>
</tr>
<tr>
<td align="left">
<input type="checkbox" name="modules" value="1" style="width:10px">
</td>
<td align="left">xxx</td>
</tr>

</tbody>
</table>
</td>


webdriver selecting checkbox script is:

driver.findElement(By.xpath("//input[@name='modules' and @value='3']")).click(); 
driver.findElement(By.xpath("//input[@name='modules' and @value='2']")).click();    
driver.findElement(By.xpath("//input[@name='modules' and @value='1']")).click();
 

Monday, 17 February 2014

Button Click In Selenium Webdriver

1.if button type is

Html Code : <button type="submit"> Login </button>
path: driver.findElement(By.xpath("//button[@type='submit']")).click();

2.if button type is

Html Code : <input type="submit" value="SUBMIT" name="action"></input>
path:  driver.findElement(By.cssSelector("input[type='submit']")).click();

Friday, 29 November 2013

Selenium Webdriver Interview Questions With Answers

1.  What is webdriver?
 WebDriver is a simpler, more concise programming interface in addition to addressing some limitations in  the Selenium-RC API. Selenium-WebDriver was developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded. WebDriver’s goal is to supply a well-designed object-oriented API that provides improved support for modern advanced web-app testing problems.
2.      What are the advantages of selenium2.0/webdriver?
    •         Need no server to start
    •     Easy to code
    •         Has sophisticated API to support wide verity of browsers.
    •     Supports to test dynamic UI web apps.

    3.      Difference between the selenium1.0 and selenium 2.0?
    Selenium 1.0
    Selenium 2.0/Webdriver
    1.      It ‘injected’ javascript functions into the browser when the browser was loaded and then used its javascript to drive the AUT within the browser.

    2.      Selenium server need to start to run tests
    3.      Has some loop-holes in supporting complex UI apps,Javascript security
    4.      No support for headless broswers
    1.      WebDriver makes direct calls to the browser using each browser’s native support for automation


    2.      Not needed unless tests are run on local machine.
    3.      Supports even drag and drop features and no security loop-holes
    4.      Supports htmlunit driver –headless browser runs fast



    4.      What are the Locators are there in selenium 2.0?
    It supports locators based on Id,name,xpath,dom,css,class,tagname
    5.      How to handle the Ajax Applications in Web driver?
    There are 2 types of waits webdriver supports to handle ajax applications to make webdrive sync to execution:
    Implicit wait :
    driver.manage().timeouts().implicitlyWait(20,TimeUnit.SECONDS);
    Explicit wait:   WebDriverWait, FluentWait
    WebElement strr = (new WebDriverWait(driver,30)).until(ExpectedConditions.presenceOfElementLocated(By.xpath("//*[starts-with(@id,'yui_3_4_0_1_137509')]/ul/li[2]/a")));
    This link explains better about handling ajax in webdriver.

    5.      How to handle the multiple windows in web driver?
                driver.switchTo().window(Window ID);

    6.      Difference between findelement() and findelements()?
         findELement will find the first matching element.

         findELements will all the matching elements. You'll probably need to loop through all the elements  returned.
    7.      How to handle the alerts in web driver?
        driver.switchTo().alert().accept();
     
    8.      How to take the screen shots in seelnium2.0?
                File src2 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
                FileUtils.copyFile(src2,new File("d:\\sc2.jpeg"));
    9.      What is web driver architecture?
    Below link gives better idea of webdriver architecture:
    http://www.aosabook.org/en/selenium.html
    10.  What is the limitations of web driver?
    • Can not automate desktop applications, supports only web applications
    •  No inbuilt commands to generate good reports
    • Cannot support readily for new browsers

    Saturday, 19 October 2013

    Data Driven Testing in WebDriver Using jxl

    Data Driven Testing through WebDriver using jxl
    Prerequisite 
    1- Download jxl jar file and add it in to path
    2- Junit jar file
    3- Selenium-server-standalone-2.x.jar
    Add these three jar file in build path  .
    In general when we say Data Driven then only thing that should come in to mind is thatinput is going to be read from some xls file, xml,csv or some other table oriented file and might be output would also be written in xls,xml or csx file. All read data from various files are stored in variables and finally used by scripts to run the test cases.
    Data Driven testing is mainly divided in two part
    1- Reading data and storing in to variable
    2- Using data stored in variable in to some generic script.
    Reading Data and storing in to variableSince Webdriver don’t have structure like other automation tools like QTP to have its own Data table to store data to run tests in Data Driven framework. So we normally two jar file(Binaries) JXL(Java Excel API) and Apache POI to make Data Driven test framework for  WebDriver.
    I am using JXL binary in this example. so for reading data from xls file we need to follow these step
    1- Opening Excel file , so to open excel file we would use these two line. I have created one excel file r.xls and now we would write code to reach to this file and to open this excel sheet.
    import java.io.FileInputStream;
    import jxl.Workbook;
    FileInputStream fi = new FileInputStream(“C:\\Users\\kaushal\\Desktop\\r.xls”);
    Workbook w = Workbook.getWorkbook(fi);
    In above code
    FileInputStream obtains input bytes from a file in a file system
    2- Opening worksheet
    import jxl.Sheet;
    Sheet sh = w.getSheet(0); or w.getSheet(Sheetnumber)
    Here 0 as argument states about firest sheet, if we want to read second sheet then we may use 1 in place of 0
    3- Reading data
    code used is 
    String variable1 = s.getCell(column, row).getContents();
    These are 3 steps that is used in reading data from excel sheet
    In my example or code I am going to write code to test login to Gmail for various user and Username and Password is save in excel sheet and we will keep reading this data by using loop and we would store these data in two variable username and password
    package learing;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import jxl.JXLException;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.read.biff.BiffException;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    public class Data{
    public static void main(String args[]) throws IOException, JXLException,BiffException,FileNotFoundException, InterruptedException{   
    WebDriver driver = new FirefoxDriver();
    Sheet s;
    
    FileInputStream fi = new FileInputStream("D:\\data1.xls");
    System.out.println("Brahmi");
    Workbook w = Workbook.getWorkbook(fi);
    s = w.getSheet(0);
    for(int row=1; row <=s.getRows();row++)
    {
    String username = s.getCell(0, row).getContents();
    System.out.println("Username "+username);
    driver.get("http://www.gmail.com");
    driver.findElement(By.name("Email")).sendKeys(username);
    String password= s.getCell(1, row).getContents();
    System.out.println("Password "+password);
    driver.findElement(By.name("Passwd")).sendKeys(password);
    Thread.sleep(10000);
    driver.findElement(By.name("signIn")).click();
     System.out.println("Waiting for page to load fully...");
    Thread.sleep(30000);
    }
    driver.quit();
    }
    
    }

    Important set of test cases for Login Page

    Here is the set of test cases that would help you in getting the right job with a better understanding of a login page.
    • Test with correct username and password: This is the most basic positive test cases, while using this user should successfully logged in.If correct username and password is not helping you to login in to your application then file a bug because this shows that something is wrong with application.
    • Test with incorrect username or password: access should be denied
    • Test with correct username and empty password:  In this case when user click on login button then a message should flash that says “Enter a password or something unexpected went wrong”
    • Test with empty username and correct password: Again an error message should appear to enter a valid email or username
    • Verify the correct error messages like  Incorrect combination of user name and password. If you are getting anything like Incorrect username or Incorrect password then be conscious because you application is giving half the information to hacker and your application is in great danger.
    • Verify that back button is not able to push you to your logged in page just after you logout from your specific account:  This kind of test cases invoke the flaw associated with Session management. When session is not closed just after your log-out means any one can access you account if you have opened your specific login enabled account at any time just by clicking Back button in Browser.So one way to save your account from such misconducts is to close browser when ever you log-out from your account.
    • Test a page url without login to application: For such thing login with correct username and correct password and go to certain page, copy the url and paste the same in another browser.if you are able to open the page then this application is not in good shape to protect the user’s information because anyone can open a specific page just by entering direct url.
    • Verify the session timeout: This is most important test case for any finance related site.Session should time out if user is inactive for few minute. This is normally a sustainability test of session. If your application is not prompting for session timeout then think once this may be issue.
    • Verify https in url for login page:  S with Http mean secure http. If login is associated with http in url means you information to login in to application is not secure and anyone can access your information just by doing small effort. While HTTPS ensure encryption of information that is being sent to server from client end.
    • Verify ID in url while processing your request :  keep track on ID associated with your request url and ID associated with request url should be dynamic not static otherwise this may help some hacker to nab your information.
    • Verify deletion of ID while browsing : Go to the place where cookies are saved and try to delete cookies when you are just browsing your account and try to find out the cookies that have your username and password because as soon as you delete that cookies you should be reached to login page. If you find the same cookies then try to change the numbers in cookies and should verify what is happening, hopefully corrupt cookies that have your real id should redirect you on login page once again even you haven’t deleted the cookies.
    • Try to login when your cookies are disable
    • Check SQL injection: most devastating vulnerabilities to  impact a business, as it can lead to exposure of all of the sensitive information stored in an application’s database, including handy information such as usernames, passwords, names, addresses, phone numbers, and credit card details.
      So I would suggest including this test cases if you are going to test some banking and insurance related application. Most common SQL injection that is used  or ‘1’=’1, if this got executed then be ready for the loss of your important information. If means hacker can login without any problem to system or application.
    • Verify account lock out: I would like to include this test case with priority, if user is using 3 or some specific number of time a wrong password then his/her account should be locked out and access should be allowed after certain assurance form filling or by calling customer care. This may help user from hackers hand.
    • Verify simultaneous login to application on different browser: I think you all would be familiar with this in daily life if you would have used railway ticket booking site.
    • Try some hit and trial username and password : before deploying application, username and password like Admin:Admin , Guest:Guest, some username :password, author:author  should be use to test but should be denied when application is deployed.
    These all test cases not only test the functionality but also test the security of the application. But I am not saying that these test cases are enough to test security of application. Because security testing is the part of testing that does not have any relation with requirement document so more and more effort is needed if we are talking about the security of an application.
    Hope this would help you in testing login page

    Thursday, 3 October 2013

    Handling Multiple Windows in WebDriver

    Dealing with multiple windows in Automation Testing has always been a little tricky and require an extra effort.
    Before commencing, let us first consider a few situations when we are likely to deal with multiple windows.
    • Filling forms may require to select the date from a separately opened window.
    • Clicking on some link/button can kick-off yet another window.
    • Handling Advertisement windows
    Hence, we can come up with various scenarios depending upon the application.
    Now let us motion ourselves towards the challenge we face under above situations. The most particular of all is switching the focus from one window to another. Let us understand the same in the following way:
    1Comprehending from the above figure, the entire process can be fundamentally segregated into following steps:
    Step 1 : Clicking on Link1 on Window A
    A new Window B is opened.
    Step 2 : Move Focus from Window A to Window B
    Window B is active now
    Step 3 : Perform Actions on Window B
    Complete the entire set of Actions
    Step 4 : Move Focus from Window B to Window A
    Window A is active now
    These are the steps which we can easily interpret out of the diagram, but there are a few more steps to add to complete this process and making our script execute. These steps don’t have visibility but plays a very vital role. Let us now re-consider the same scenario.
    Step 1 : Clicking on Link1 on Window A
    A new Window B is opened.
    Step 2 : Save reference for Window A
    Step 3 : Create reference for Window B
    Step 3 : Move Focus from Window A to Window B
    Window B is active now
    Step 3 : Perform Actions on Window B
    Complete the entire set of Actions
    Step 4 : Move Focus from Window B to Window A
    Window A is active now
    Let us understand the same with a small coding example.
     public class MultiWindowHandle {  
     WebDriver driver;  
     @Before  
     public void setup() throws Exception {  
     driver=new FirefoxDriver();  
     String URL="https://www.abc.co.in/";   
     driver.get(URL);  
     driver.manage().window().maximize();  
     }  
     @Test  
     public void test() throws Exception {   
     // Opening Calender  
     driver.findElement(By.xpath("//img[@alt='Calender']")).click();  
     // Storing parent window reference into a String Variable  
     String Parent_Window = driver.getWindowHandle();    
      // Switching from parent window to child window   
     for (String Child_Window : driver.getWindowHandles())  
     {  
     driver.switchTo().window(Child_Window);  
     // Performing actions on child window  
     driver.findElement(By.id("calendar_month_txt")).click();  
     List  Months=driver.findElements(By.xpath("//div[@id='monthDropDown']//div"));  
     int Months_Size=Months.size();  
     System.out.println("Month size is:"+Months_Size);  
     Months.get(1).click();  
     driver.findElement(By.xpath("//*[@id='calendarDiv']/div/table/tbody/tr/td[contains(text(),'16')]")).click();  
     }  
     //Switching back to Parent Window  
     driver.switchTo().window(Parent_Window);  
     //Performing some actions on Parent Window  
     driver.findElement(By.className("btn_style")).click();  
     }  
      @After  
      public void close() {  
      driver.quit();  
      }   
     }  

    Angular JS Protractor Installation process - Tutorial Part 1

                         Protractor, formally known as E2E testing framework, is an open source functional automation framework designed spe...