0

Please forgive me as I am still very new to the world of test automation. I have started out by using Selenium WebDriver with JUnit4, predominately on windows OS, although I have modified my scripts and ran them on Mac.

I want to be able to create a set of classes containing set data such as usernames, passwords, default url . Perhaps even calling them from an excel file, but for now Im happy to store the data in classes and then pass that data into other test classes. Im guessing this would be a framework of some sort.

Currently I am writing classes that all begin with something like:

public class ExampleSQATest{

    public static Chromedriver chrome;

    @BeforeClass
    public static void launchBrowser(){
    System.setProperty("webdriver.chrome.driver", "chromedriver/chromedriver.exe");

    chrome = new ChromeDrievr();
    }
    @Test
    public void aLogin(){

    chrome.manage().window().maximize();

    chrome.navigate().to("http://mydummywebsite.com");

    new WebDriverWait(chrome, 10).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector
                    ("input#UserName")));

    WebElement username = chrome.findElementByCssSelector("input#UserName");

    username.sendKeys("username");

    WebElement password = chrome.findElementByCssSelector("input#Password");
    password.sendKeys("password");
    WebElement submit = chrome.findElementByCssSelector("input[type='submit']");
    submit.click();
    }
}

I will then proceed to write further test methods which requires entering data, but I'd like to be able to call this data from somewhere else that is already predefined.

Can anyone provide any suitable suggestions to investigate so I can learn. Something that is a guide or tutorial. Nothing too advanced, just something that helps me get started by advising me how to set a class of methods to be called by other classes and how it all links together as a framework.

Many thanks in advance.

1 Answer 1

2

@Django_Tester: If you want to use your set of data such as usernames, passwords, default url, etc in one class and calling these data into another class, then store your data in a string.

String un = "s";
String pwd = "s";
String baseurl = "your test url";

And below is the sample code:

import java.util.concurrent.TimeUnit;
import org.junit.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
public class Parameters {
    public static WebDriver d;
    String un = "s";
    String pwd = "s";
    String baseurl = "your test url";
    @Test
    public void m1() {
         d = new FirefoxDriver();  
            d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        d.get(baseurl);
        d.findElement(By.name("username")).sendKeys(un);
        d.findElement(By.name("pwd")).clear();
        d.findElement(By.name("pwd")).sendKeys(pwd);
    }
}

And using above set of data in another class (Below code):

import java.util.concurrent.TimeUnit;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Para extends Parameters{
    public static WebDriver d;
    @Test
    public  void m2() {
         d = new FirefoxDriver();  
            d.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        d.get(baseurl);
        d.findElement(By.name("username")).sendKeys(un);
        d.findElement(By.name("pwd")).sendKeys(pwd);
    }
}

May this helps for you:)...

Your Answer

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

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