Pokazywanie postów oznaczonych etykietą specflow. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą specflow. Pokaż wszystkie posty

czwartek, 14 marca 2019

SpecFlow with Coypu and custom IWebDriver for Coypu SessionConfiguration

Using Coypu with SpecFlow might have some limitations. Sometimes there is need to use driver with options or capabilities. Coypu gives possibility to customise web drivers with SessionConfiguration class.

Template

Example of Coypu custom web driver configurations is prepared in specflow-coypu-custom-webdriver template:

Custom Coypu driver configuration

To customise, for example, chrome driver, create class like below:

public class CustomChromeWebDriver : SeleniumWebDriver
    {
        public CustomChromeWebDriver(Browser browser) : base(CustomProfile(), browser)
        {
        }

        private static IWebDriver CustomProfile()
        {
            ChromeOptions chromeOptions = new ChromeOptions();
            chromeOptions.AddArguments("test-type");
            chromeOptions.AddArguments("--disable-extensions");

            return new ChromeDriver(chromeOptions);
        }
    }

Use name of created class for 'Driver' declaration in Coypu session configuration like below:

var sessionConfigurationForChrome = new SessionConfiguration()
{
Driver = typeof(CustomChromeWebDriver),
Browser = Coypu.Drivers.Browser.Chrome,
AppHost = "https://specflow.org/", // whatever url you want
Timeout = TimeSpan.FromSeconds(2)
};
browser = new BrowserSession(sessionConfigurationForChrome);
browser.MaximiseWindow();

Specflow-coypu-custom-webdriver template has BrowserSessionManager class to manage custom drivers for chrome, firefox, internet explorer and headless mode for chrome and firefox.

Custom RemoteWebDriver configuration

If you need to use RemoteWebDriver for running tests, in custom web driver classes replace IWebDriver to RemoteWebDriver (there might be also necessity to add option for running tests locally).

wtorek, 12 marca 2019

SpecFlow with Selenium IWebDriver manager

Specflow-webdriver-manager template uses Selenium IWebDriver for running browsers.

Template

Template you will find on GitHub:
https://github.com/bazylMN/specflow-webdriver-manager

WebDriverManager for browsers

Choice between browsers is managed by 'GetDriver()' method in WebDriverManager class:

public IWebDriver GetDriver()
        {
            var browserType = TestContext.Parameters["BROWSER"];
            switch (browserType)
            {
                case "CHROME":
                    webDriver = new ChromeDriver();
                    break;

                case "CHROMEHEADLESS":
                    ChromeOptions chromeOptions = new ChromeOptions();
                    chromeOptions.AddArguments("headless");
                    webDriver = new ChromeDriver(chromeOptions);
                    break;

                case "INTERNETEXPLORER":
                    webDriver = new InternetExplorerDriver();
                    break;

                case "FIREFOX":
                    webDriver = new FirefoxDriver();
                    break;

                case "FFHEADLESS":
                    FirefoxOptions firefoxOptions = new FirefoxOptions();
                    firefoxOptions.AddArguments("--headless");
                    webDriver = new FirefoxDriver(firefoxOptions);
                    break;

                default:
                    webDriver = new ChromeDriver();
                    break;
            }
            return webDriver;
        }

Commands for running particular browser

Default browser is Chrome, but you can choose Firefox, Internet Explorer or headless mode for Chrome and Firefox, using commands:

nunit3-console specflow-webdriver-manager\bin\Debug\specflow-webdriver-manager.dll --params:BROWSER=FIREFOX --work=NUnitTestResult --out=NUnitTestResult.txt --result=NUnitTestResult.xml

nunit3-console specflow-webdriver-manager\bin\Debug\specflow-webdriver-manager.dll --params:BROWSER=INTERNETEXPLORER --work=NUnitTestResult --out=NUnitTestResult.txt --result=NUnitTestResult.xml

nunit3-console specflow-webdriver-manager\bin\Debug\specflow-webdriver-manager.dll --params:BROWSER=CHROMEHEADLESS --work=NUnitTestResult --out=NUnitTestResult.txt --result=NUnitTestResult.xml

nunit3-console specflow-webdriver-manager\bin\Debug\specflow-webdriver-manager.dll --params:BROWSER=FFHEADLESS --work=NUnitTestResult --out=NUnitTestResult.txt --result=NUnitTestResult.xml

Instruction

Instruction of usage specflow-webdriver-manager template is placed in README.md file.

SpecFlow with Coypu and Coypu BrowserSession manager

SpecFlow UI tests might be run with Selenium IWebDriver or, you might use Coypu framework (https://github.com/featurist/coypu).
With Coypu you can manage browsers and interact with elements on page. After Coypu description:
"If your tests are littered with sleeps, retries, complex XPath expressions and IDs dug out of the source with browser developer tools then Coypu might help."

Template

Specflow-coypu template with Coypu BrowserSession manager you will find on GitHub:

Coypu configuration for browsers

Choice between browsers is managed by 'GetBrowser()' method in BrowserSessionManager class:

public BrowserSession GetBrowser()
        {
            if (browser != null)
                return browser;

            var sessionConfiguration = new SessionConfiguration()
            {
                Browser = GetBrowserName(),
                AppHost = "https://specflow.org/", // whatever url you want
                Timeout = TimeSpan.FromSeconds(2)
            };
            browser = new BrowserSession(sessionConfiguration);
            browser.MaximiseWindow();

            return browser;
        }

private Browser GetBrowserName()
        {
            var browserType = TestContext.Parameters["BROWSER"];
            switch (browserType)
            {
                case "CHROME":
                    browserName = Coypu.Drivers.Browser.Chrome;
                    break;

                case "INTERNETEXPLORER":
                    browserName = Coypu.Drivers.Browser.InternetExplorer;
                    break;

                case "FIREFOX":
                    browserName = Coypu.Drivers.Browser.Firefox;
                    break;

                default:
                    browserName = Coypu.Drivers.Browser.Chrome;
                    break;
            }
            return browserName;
        }

Comands for running particular browser

Default browser in template is Chrome, but you can choose Firefox or Internet Explorer, using command:

nunit3-console specflow-coypu\bin\Debug\specflow-coypu.dll --params:BROWSER=FIREFOX --work=NUnitTestResult --out=NUnitTestResult.txt --result=NUnitTestResult.xml

nunit3-console specflow-coypu\bin\Debug\specflow-coypu.dll --params:BROWSER=INTERNETEXPLORER --work=NUnitTestResult --out=NUnitTestResult.txt --result=NUnitTestResult.xml

Instruction

Instruction of usage is placed in README.md file.

wtorek, 19 lutego 2019

[QUICK TIP] SpecFlow/Visual Studio: how to handle with error "The geckodriver.exe file does not exist in the current directory or in a directory on the PATH environment variable"

At the start of work with SpecFlow framework in Visual Studio, you might get error like above, while running first test, which needs to initialise browser.

After adding driver .exe file to your testing project (for example chromedriver.exe, geckodriver.exe), in Visual Studio it is needed to change properties of .exe file.

Simply change "Copy to Output Directory" to "Copy always".