3

I have created test cases for email but there are multiple negative test cases for email field then how should I pass multiple values in the input field. I have created function.but don't is that should I have to create more than one function for that?

@DataProvider(name = "Authentication1")
    public static Object[][] credentials1()
    {

        return new Object[][] { { "12234", " %&^%","gfdgfdg"," dfg","gfdg "} };

    }
@Test(dataProvider = "Authentication1" )
    public void invaliddata(String fname, String lname,String email,String pass,String cpass)
    {
        driver.get("http://google.com");

        WebElement firstname = driver.findElement(By.id("FirstName"));
        firstname.clear();
        firstname.sendKeys(fname);

        WebElement lastname = driver.findElement(By.id("LastName"));
        lastname.clear();
        lastname.sendKeys(lname);

        WebElement mail = driver.findElement(By.id("Email"));
        mail.clear();
        mail.sendKeys(email);

        WebElement password = driver.findElement(By.id("Password"));
        password .clear();
        password .sendKeys(pass);

        WebElement conpassword = driver.findElement(By.id("ConfirmPassword"));
        conpassword .clear();
        conpassword.sendKeys(cpass);

        WebElement register = driver.findElement(By.id("register-button"));
        register.click();
1
  • instead of multiple time webelement, write only one webelement which contain the parameter and call the method eg. invaliddata(String fname, String lname,String email,String pass,String cpass, string id) driver.findElement(By.id(id));
    – Ankit jain
    Feb 23, 2017 at 22:18

1 Answer 1

4

In this scenario; you have two options to test your Authentication scenario for multiple test data's:

1) Use Data provider with two-dimensional java object
2) Passing data to DataProvider from Excel sheet

1) Use Data provider with two-dimensional java object:


Data provider returns a two-dimensional java object to the test method and the test method will invoke M times in a M*N type of object array. For example, if the DataProvider returns an array of 2*3 objects, the corresponding test case will be invoked 2 times with 3 parameters each time.

    public class Dataprovider
    {

        WebDriver driver;

        @BeforeTest
        public void setup()
        {
             driver = new FirefoxDriver();
             driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
             driver.get("https://google.com");

        }

        @Test(dataProvider="SearchProvider")
        public void testMethod(String author,String searchKey) throws InterruptedException{
            {

                WebElement searchText = driver.findElement(By.name("q"));
                searchText.sendKeys(searchKey);
                System.out.println("Welcome ->"+author+" Your search key is->"+searchKey);

                String testValue = searchText.getAttribute("value");
                System.out.println(testValue +"::::"+searchKey);
                searchText.clear();

                //Verify if the value in google search box is correct
                Assert.assertTrue(testValue.equalsIgnoreCase(searchKey));
            }

        }

        /**
         * @return Object[][] where first column contains 'author'
         * and second column contains 'searchKey'
         */

        @DataProvider(name="SearchProvider")
        public Object[][] getDataFromDataprovider()
        {
            return new Object[][] 
            {
                { "India", "Delhi" },

                { "US", "Los Angles" },

                { "japan", "Tokiyo" }

            };

        }

    }

2) Passing data to DataProvider from Excel sheet:

DataProvider helps to send multiple sets of data to a test method. But here we need to make sure that the array returned by the dataprovider should match with the test method parameters.

Take a simple program in which we will validate login screen by taking multiple usernames and passwords. The annotated method must return object[][] where each object[] can be assigned to the test method one as username and the other parameter as password.

public class ReadExcelDataProvider {
    public WebDriver driver;
    public WebDriverWait wait;
    String appURL = "Your baseUrl";

    //Locators
    private By byEmail = By.id("username");
    private By byPassword = By.id("session_password-login");
    private By bySubmit = By.id("signin");

    @BeforeClass
    public void testSetup() {
        driver=new FirefoxDriver();
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, 5);
    }

    @Test(dataProvider="empLogin")
    public void VerifyInvalidLogin(String userName, String password) 
    {
        driver.navigate().to(appURL);
        driver.findElement(byEmail).sendKeys(userName);
        driver.findElement(byPassword).sendKeys(password);

        //wait for element to be visible and perform click
        wait.until(ExpectedConditions.visibilityOfElementLocated(bySubmit));
        driver.findElement(bySubmit).click();

        //Check for error message
        wait.until(ExpectedConditions.presenceOfElementLocated(byError));
        String actualErrorDisplayed = driver.findElement(byError).getText();
        String requiredErrorMessage = "Please correct the marked field(s) below.";
        Assert.assertEquals(requiredErrorMessage, actualErrorDisplayed);

    }

    @DataProvider(name="empLogin")
    public Object[][] loginData() 
    {
        Object[][] arrayObject = getExcelData("D:/sampledoc.xls","Sheet1");
        return arrayObject;
    }

    public String[][] getExcelData(String fileName, String sheetName) 
    {
        String[][] arrayExcelData = null;
        try {
            FileInputStream fs = new FileInputStream(fileName);
            Workbook wb = Workbook.getWorkbook(fs);
            Sheet sh = wb.getSheet(sheetName);

            int totalNoOfCols = sh.getColumns();
            int totalNoOfRows = sh.getRows();

            arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];

            for (int i= 1 ; i < totalNoOfRows; i++) {

                for (int j=0; j < totalNoOfCols; j++) {
                    arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        return arrayExcelData;
    }

    @Test
    public void tearDown() {
        driver.quit();
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.