0

Here, i just opened the url in FF browser using this script..If i want to run another script,do i need to create project, package and class for that ?I'm new to selenium web driver..Can you guys  please help.. [Here, i just opened the url in FF browser using this script..If i want to run another script,do i need to create project, package and class for that ?I'm new to selenium web driver..Can you guys please help..

1

In your example you run your script within main method that is an entry point of the whole Java program. You do not need to create a separate project of course. Regarding other aspects it might depend on how you'd like to organize your code.

Technically you may go in one of the following ways (just the examples):

  • describe different scripts in different methods and then call them from main method
  • Have different classes each with their own main method holding the script
  • Utilize some unit test frameworks like JUnit or TestNg and keep different scripts in different methods of your test classes (which have to be marked in some special way)

Whatever way you will use you should also consider the practices of code re-usage so that you define some common code (like creating web driver) and script code that would re-use that common-code.

I can suggest the following architecture if you run the tests from main:

1. Implement interface:

public interface IStep {

    void take(WebDriver driver);

}

2. Implement test runner with tests (tests can actually be decoupled from the runner, however not to waste time I coupled them)

public class Test {

    WebDriver driver;

    public static void main(String[] args) {
        new Test().takeStep((wd)->{test1(wd);});
        new Test().takeStep((wd)->{test2(wd);});
    }

    private void before(){
        System.out.println("Starting up test..");
        System.setProperty("webdriver.chrome.driver", "E:/Dev/WebDrivers/chromedriver.exe");
        driver = new ChromeDriver();
    }

    private void after(){
        System.out.println("Shutting down test..");
        if(driver != null){
            driver.quit();
        }
    }

    public void takeStep(IStep step){
        before();
        try{
            step.take(driver);
            System.out.println("Test Passed");
        }catch (Exception e){
            System.err.println("Test Failed: " + e.getMessage());
            e.printStackTrace();
        }finally {
            after();
        }
    }

    public static void test1(WebDriver wd){
        wd.get("http://google.com");
        System.out.println("Search input in Google");
        wd.findElement(By.xpath("//input"));
    }

    public static void test2(WebDriver wd){
        wd.get("http://sqa.stackexchange.com");
        System.out.println("Search input in SO");
        wd.findElement(By.xpath("//input"));
    }

}

In this example to add new test you should do the following (for example we'd like to add test3):

Add new method with your new test code

public static void test3(WebDriver wd){
    // Your another script
}

Add execution to the main

new Test().takeStep((wd)->{test3(wd);});

P.S. - You need to learn some Java concepts to completely understand this code, such as:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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