开发者

How to run multiple testcases by running one selenium server

开发者 https://www.devze.com 2023-03-30 11:53 出处:网络
I have 5 test suites (ie, multiple classes), each suite has its own separate server.start() and stop() as well as selen开发者_Go百科ium.start() and stop(). Now, what I want is: I want my script to run

I have 5 test suites (ie, multiple classes), each suite has its own separate server.start() and stop() as well as selen开发者_Go百科ium.start() and stop(). Now, what I want is: I want my script to run all test suites without stopping the server. If you want to say in other words, I need to run multiple test suites in one session. Can anyone help me how to do that?


I would really like to know what test framework you are using like Varun asked. If you are using Python you could look at this example which runs any test file with the ending of _test.py.

#!/usr/bin/python27

import os
import sys

failure = False
f = open('test.log', 'w')
xmlHeader = """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> """
f.write(xmlHeader)

for dirname, dirnames, filenames in os.walk('.'):
    for filename in filenames:
        if filename.endswith("_test.py"):
            filename = "python27 %s/%s" % (dirname, filename)
    testLine = "Running test: " + filename
    seperator = "---------------------------------------------------------"
    print seperator
            print testLine
            print seperator
    f.write("<testsuite name=\"" +testLine + "\">")
            out = os.popen(filename)
    outputString = out.read()
            print outputString
    f.write("""<system-out><![CDATA[""" + outputString +  "]]></system-out>")
            retval = out.close()
            if retval:
                failure = True
        failString = "FAILURE - test file: " + filename
        print failString
        f.write("<failure desc=\"" + failString +"\"/>") 
    f.write("</testsuite>")
f.write("</html>")


if failure:
    sys.exit(1)

You may also want to look into setting up something similar to Jenkins http://jenkins-ci.org/ which can run different test suites.


Try the following things. This wrt Junit I am telling.

  1. Create a Base Class where you define 4 methods with the annotations @BeforeClass,@AfterClass,@Before,@After.
  2. Add your server start and stop in the Before/AfterClass. In BeforeClass check once if the server is already running or not. If not then only start.
  3. Add selenium start and stop commands in @Before and @After.
  4. Extend this BaseClass in all your Test classes.Try running your suites.

Reply in case of any issues. Its better to use TestNg as it gives you more flexibility in handling and executing testcases.


you can remove junit and test suite dependancy from your suite I am using selenium without any junit and test suite Add a class containing main function and add function calls to all classes of your suite Define a selenium object like this selenium=new DefaultSelenium(serverHost,serverPort,browserStartCommand,browserURL); selenium.start(); after your all test executes add selenium.stop();


To do this programatically you need to create a static variable to hold the SeleniumServer instance.

public class SeleniumServerManager {

    private static SeleniumServer seleniumServer;

    public static void attemptToStartSeleniumServer() throws Exception {
        if (null == seleniumServer) {
            seleniumServer = new SeleniumServer();
            seleniumServer.start();
        }
    }
}

You'll have to reference this in all your test suites to ensure the server is started

public class StackOverflowTest extends TestCase {

    private Selenium selenium;

    @Override
    public void setUp() {
        SeleniumServerManager.attemptToStartSeleniumServer();
        this.selenium = new DefaultSelenium("localhost", 4444, "*firefox", "http://www.stackoverflow.com");
        this.selenium.start();
    }

    public void testStackoverflow() {
        this.selenium.open("/");
    }
}

If you don't like the repeated call to #attemptToStartSeleniumServer and are using either Ant or Maven in your project, have a look at the available Ant task/Maven plugin which can manage your Server instance.


Adding to the discussion you can use Testng framework to your automation test. Where you can have one Class which will setup and teardown selenium instance after all the tests are completed.

class something{
    public static Selenium selenium;
    public static Selenium globalSeleniumInstance

@BeforeSuite(alwaysRun = true)
public void init(){
selenium = new DefaultSelenium()
    globalSeleniumInstance = selenium;
        selenium.start();
}
@AfterSuite(alwaysRun = true)
    public void destroy() throws Exception {
        selenium.stop();
    }

Now for you test class you can 

@Test(groups = {"myWebsite"}, alwaysRun = true)
public class MyWebsite {
  private Selenium selenium;

  @BeforeClass(alwaysRun = true)
  public void init() {
    selenium = ResourceManager.globalSeleniumInstance;
    selenium.windowFocus();
    selenium.windowMaximize();
  }

  @Test(alwaysRun = true)
  public void lookForRecentPosts() throws Exception {
do something with selenium here
}
      }

so in you testng xml file you can add the classes like these
<suite thread-count="1" verbose="1" name="Test Automation Suite"
    annotations="JDK" parallel="false">
<test name="Tests" junit="false">
classes>
            <class name="com.test.managerclass" />
</classes>
    </test>

</suite>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号