Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configuration File for Driving Selenium

I have about 500 possible paths to a particular page, and I need to test all of them. Each path to the that page looks similar to this (using PHP web driver; usually has about 10 steps):

// Navigate to form
$driver->get('http://www.domain.com');
$driver->get($driver->findElement(WebDriverBy::xpath("//a[contains(text(),'Foo 1')]"))->getAttribute('href'));
$driver->findElement(WebDriverBy::xpath("//div[@class='countryHeader']//a[contains(text(), 'Bar 1')]"))->click();
$driver->findElement(WebDriverBy::xpath("//form[@name='formDisclaimer']//input[contains(@class, 'button')]"))->click();

I don't want to have to write code for all the steps for all possible paths to the page. I do, however, have all the pertinent details of the steps (e.g. the XPath, the string the node may contain, etc.) in a database.

Is there a way for me to "dynamically" produce some sort of configuration file (either in XML or JSON) that I can feed to the driver as a set of instructions for it to follow?

like image 893
StackOverflowNewbie Avatar asked Sep 17 '16 08:09

StackOverflowNewbie


People also ask

What is configuration file in Selenium?

What is the use of config.properties file in selenium. Properties are used to externalize the data which is configurable and if you put that data in your code (test script) you have to build the code each time you want to change the value of the property.


3 Answers

A long time back at one of my project I had a similar requirement. I tried to create a Robot (or someone may call Web Crawler). As I started navigating through the pages I started maintaining the navigation paths in spreadsheet, so I don't have to click on the paths manually. Once I have the paths, next time whenever a Path changes I will be notified and if it is a valid change then make that change in s/s or raise it as a bug.

like image 194
Mrunal Gosar Avatar answered Oct 17 '22 16:10

Mrunal Gosar


As you said you have all relevant details in the database then you just can simply read it and in a foreach loop pass to selenium driver.

or if you don't want to have a reference to the database in your test, just dump data to PHP array and add to your test class.

You just need to write a logic to transform your sql data into test. Don't need to write every test manually.

I don't know which testing framework you are using, but you can execute many tests from a single test for example in PHPUnit that would be something like:

class My_PathsTest extends PHPUnit_Framework_TestCase
{

    public function setUp() {
      // setup $this->tests here 
    }

    public function testAll() {
        // $this->tests would contain info about paths taken from database.

        $failures = array();

        foreach($this->tests as $paths_set) {
            try {

                /**
                 * $driver->get($paths_set['start_point']);
                 * foreach ($paths_set['paths'] as $path ) {
                 *  $driver->findElement(WebDriverBy::xpath($path));
                 * }
                 * 
                 * Important!!!
                 * If you didn't find something you expected 
                 * just throw the PHPUnit_Framework_ExpectationFailedException exception
                 * throw new PHPUnit_Framework_ExpectationFailedException('Element missing add some info here about which is missing etc..');
                 */

            }

            catch(PHPUnit_Framework_ExpectationFailedException $e) {
                $failures[] = $e->getMessage();
            }
        }

        if (!empty($failures)) {
            throw new PHPUnit_Framework_ExpectationFailedException(count($failures) . " assertions failed:\n\t" . implode("\n\t", $failures));
        }
    }

}
like image 28
Pawel Wodzicki Avatar answered Oct 17 '22 15:10

Pawel Wodzicki


best is to get data from db with odbc as a list (array) xpath locators and then loop over it. If you don't have a direct access to the db, export the query results as a .csv file (MS db has an option save as, not sure about the others) and then read the file and loop over the array

like image 21
rafalf Avatar answered Oct 17 '22 14:10

rafalf