import os
import pytest

import watir_snake
from watir_snake.browser import Browser
from watir_snake.support.webserver import WebServer

browsers = (
    'chrome',
    'edge',
    'firefox',
    'ie',
    'phantomjs'
    # 'remote',  TODO: just local for now
    'safari'
)

watir_snake.default_timeout = 3


def pytest_addoption(parser):
    parser.addoption(
        '--browser',
        action='store',
        choices=browsers,
        metavar='BROWSER',
        help='browser to run tests against ({})'.format(', '.join(browsers)))


@pytest.fixture(scope='session')
def browser(request):
    kwargs = {}

    try:
        browser_name = request.config.getoption('--browser')
    except AttributeError:
        raise Exception('This test requires a --browser to be specified.')

    # if driver_class == 'remote':
    #     capabilities = DesiredCapabilities.CHROME.copy()
    #     kwargs.update({'desired_capabilities': capabilities})
    if os.environ.get('DRIVER_PATH'):
        kwargs['executable_path'] = os.environ.get('DRIVER_PATH')
    browser = Browser(browser_name, **kwargs)
    yield browser
    try:
        browser.quit()
    except:
        pass


@pytest.fixture(scope='session')
def page(browser, webserver):
    class Page(object):
        def url(self, name):
            return webserver.path_for(name)

        def load(self, name):
            browser.goto(self.url(name))
    return Page()


@pytest.fixture(autouse=True)
def start_page(request, page):
    marker = request.node.get_marker('page')
    if marker:
        page.load(marker.args[0])


@pytest.fixture(scope='session')
def messages(browser):
    class Messages(object):
        @property
        def list(self):
            return [el.text for el in browser.div(id='messages').divs()]
    yield Messages()


@pytest.fixture(autouse=True, scope='session')
def webserver():
    webserver = WebServer()
    webserver.start()
    yield webserver
    webserver.stop()

@@ -570,22 +570,22 @@
 ')%0Adef b
-rowser
+kwargs
 (request
@@ -621,28 +621,33 @@
         
+kwargs%5B'
 browser
-_name
+'%5D
  = reque
@@ -1052,42 +1052,103 @@
     
-browser = B
+yield kwargs%0A%0A%0A@pytest.fixture(scope='session')%0Adef browser(bkwargs):%0A    b
 rowser
-(b
+ = B
 rowser
-_name, 
+(
 **
+b
 kwar

move browser options to bkwargs fixture to make available during tests