GithubHelp home page GithubHelp logo

wgnet / webium Goto Github PK

View Code? Open in Web Editor NEW
158.0 28.0 36.0 1.38 MB

Webium is a Page Object pattern implementation library for Python (http://martinfowler.com/bliki/PageObject.html). It allows you to extend WebElement class to your custom controls like Link, Button and group them as pages.

License: Apache License 2.0

Python 83.60% HTML 15.93% Shell 0.46%
selenium python page-object webdriver selenium-webdriver

webium's Introduction

Webium

PyPI Python Versions Build Status

Webium is a Page Object pattern implementation library for Python (http://martinfowler.com/bliki/PageObject.html).

It allows you to extend WebElement class to your custom controls like Link, Button and group them as pages.

Installation

The latest stable version is available on PyPI:

pip install webium

Usage

Main classes are:

  • webium.Find
  • webium.Finds
  • webium.BasePage

Basic usage example:

from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from webium.controls.link import Link
from webium.driver import get_driver
from webium import BasePage, Find, Finds


class GooglePage(BasePage):
    url = 'http://www.google.com'

    text_field = Find(by=By.NAME, value='q')
    button = Find(by=By.NAME, value='btnK')


class ResultItem(WebElement):
    link = Find(Link, By.XPATH, './/h3/a')


class ResultsPage(BasePage):
    stat = Find(by=By.ID, value='resultStats')
    results = Finds(ResultItem, By.XPATH, '//div/li')


if __name__ == '__main__':
    home_page = GooglePage()
    home_page.open()
    home_page.text_field.send_keys('Page Object')
    home_page.button.click()
    results_page = ResultsPage()
    print('Results summary: ' + results_page.stat.text)
    for item in results_page.results:
        print(item.link.text)
    get_driver().quit()

More usage details are available here: http://wgnet.github.io/webium/

webium's People

Contributors

andreytishenko avatar kejkz avatar khrol avatar martinpelikan avatar sunx2ych avatar ufranske avatar wstranger avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

webium's Issues

Driver instance does not restart when new page object is instantiated after calling get_driver().quit()

We have a need to restart the browser after certain tests. However, after get_driver().quit() is called, Webium will not automatically create the driver itself in the following test. The simplest example I could come up with that reproduces the behavior can be found below. I'm not certain if this is a webium issue, or just a usage issue on my part. Any suggestions as to achieve the desired behavior would be greatly appreciated.

from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from webium.controls.link import Link
from webium.driver import get_driver
from webium import BasePage, Find, Finds

class GooglePage(BasePage):
    url = 'http://www.google.com'

def test_one():
    home_page = GooglePage()
    home_page.open()
    get_driver().quit()

def test_two():
    home_page = GooglePage()
    home_page.open()
    get_driver().quit()

test_one()
test_two()

Traceback (most recent call last):
File "github_example.py", line 21, in
test_two()
File "github_example.py", line 17, in test_two
home_page.open()
File "/Users/Code/venv/lib/python3.5/site-packages/webium/base_page.py", line 54, in open
self._driver.get(self.url)
File "/Users/Code/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 213, in get
self.execute(Command.GET, {'url': url})
File "/Users/Code/venv/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 199, in execute
response = self.command_executor.execute(driver_command, params)
File "/Users/Code/venv/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 395, in execute
return self._request(command_info[0], url, body=data)
File "/Users/Code/venv/lib/python3.5/site-packages/selenium/webdriver/remote/remote_connection.py", line 425, in _request
self._conn.request(method, parsed_url.path, body, headers)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1083, in request
self._send_request(method, url, body, headers)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 1079, in endheaders
self._send_output(message_body)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 911, in _send_output
self.send(msg)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 854, in send
self.connect()
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/http/client.py", line 826, in connect
(self.host,self.port), self.timeout, self.source_address)
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 711, in create_connection
raise err
File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/socket.py", line 702, in create_connection
sock.connect(sa)
ConnectionRefusedError: [Errno 61] Connection refused

Can't execute JavaScript on an element with Firefox

I have a page with elements which are out of the visible browser area and I need to scroll these elements to view. I try to use execute_script("return arguments[0].scrollIntoView(true);", element) method but it fails for Firefox with TypeError: <selenium.webdriver.remote.webelement.WebElement (session="732d3ce1-eaed-494c-b441-b29fdb9e02e3", element="e9db3f8c-9836-4c68-a9c9-364066a3d76e")> is not JSON serializable . However, it works fine in Chrome.

It seems the root of this problem is described in SeleniumHQ/selenium#2666 (comment). "the problem might have been that the reporter was using a custom subclass of WebElement with Firefox." - it means it can happens for the same reason for Webium which use also custom subclass when you use Selenium 3.

How do you handle this problem in your projects? Is there chance that you can implement something for Webium similar to this patch?

This example can reproduce the problem.
selenium==3.4.1
webium==1.2.1
geckodriver is 0.16

from selenium.webdriver.common.by import By
from selenium.webdriver.remote.webelement import WebElement
from webium.controls.link import Link
from webium.driver import get_driver
from webium import BasePage, Find, Finds


class GooglePage(BasePage):
    url = 'http://www.google.com'

    text_field = Find(by=By.NAME, value='q')
    button = Find(by=By.NAME, value='btnG')


class ResultItem(WebElement):
    link = Find(Link, By.XPATH, './/h3/a')


class ResultsPage(BasePage):
    stat = Find(by=By.ID, value='resultStats')
    results = Finds(ResultItem, By.XPATH, '//div/li')
    suggest1 = Find(by=By.CSS_SELECTOR, value='.card-section .brs_col:nth-child(1) ._e4b:nth-child(1) a')



if __name__ == '__main__':
    home_page = GooglePage()
    home_page.open()
    home_page.text_field.send_keys('Page Object')
    home_page.button.click()
    results_page = ResultsPage()
    print('Results summary: ' + results_page.stat.text)
    for item in results_page.results:
        print(item.link.text)
    # results_page._driver.execute_script("window.scrollBy(0, -120);")
    element = results_page.suggest1
    # try to scroll to first link with other search suggestion
    get_driver().execute_script("return arguments[0].scrollIntoView(true);", element)
    get_driver().quit()

Results summary: Результатов: примерно 11 200 000 (0,22 сек.)
Traceback (most recent call last):
File "C:/Source2/PythonProjects/untitled1/page_test.py", line 37, in
get_driver().execute_script("return arguments[0].scrollIntoView(true);", element)
File "C:\Users\user\webium_test1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 487, in execute_script
'args': converted_args})['value']
File "C:\Users\user\webium_test1\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 250, in execute
response = self.command_executor.execute(driver_command, params)
File "C:\Users\user\webium_test1\lib\site-packages\selenium\webdriver\remote\remote_connection.py", line 461, in execute
data = utils.dump_json(params)
File "C:\Users\user\webium_test1\lib\site-packages\selenium\webdriver\remote\utils.py", line 34, in dump_json
return json.dumps(json_struct)
File "C:\Python352\lib\json_init_.py", line 230, in dumps
return _default_encoder.encode(obj)
File "C:\Python352\lib\json\encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Python352\lib\json\encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "C:\Python352\lib\json\encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <selenium.webdriver.remote.webelement.WebElement (session="732d3ce1-eaed-494c-b441-b29fdb9e02e3", element="e9db3f8c-9836-4c68-a9c9-364066a3d76e")> is not JSON serializable

Dependencies problem

Hi,

Setup file have fixed dependencies for old versions of selenium and nose. Is a webium compatible with new packages?

Regards,

TypeError: Object of type 'Checkbox' is not JSON serializable

This code work in Google Chrome and raise TypeError in Mozilla Firefox:

from selenium.webdriver.remote.webelement import WebElement

class Clickable(WebElement):
      """ Clickable class from webium """
      pass

class Checkbox(Clickable):
      """ Checkbox class from webium """
      pass

This code work in Google Chrome and in Mozilla Firefox:

from selenium.webdriver.firefox.webelement import FirefoxWebElement as WebElement

class Clickable(WebElement):
      """ Clickable class from webium """
      pass

class Checkbox(WebElement):
      """ Checkbox class from webium """
      pass

Versions of tools:

webium 1.2.1           Google Chrome 68.0.3440.84 (64-бит)        Mozilla Firefox 61.0.1 (64-бит)
selenium 3.14.0        chromedriver 2.41 (32-бит)                 geckodriver 0.21.0 (64-бит)

Add a CONTRIBUTING.md for people creating pull requests.

Could I suggest adding a CONTRIBUTING.md file to the repository, which would cover general test setup and structure?

I was able to figure out how to run the tests by reading the TravisCI logs (installing Firefox-ESR, nose, etc.), but without your pointers I would have had some struggle figuring out where best to write the tests (or even that it was necessary for the PR to get merged).

ImportError: No module named controls.link

Newbie question... Installed webium 1.0.7 on Windows workstation running Python 2.7.9. When trying to run the basic usage example I get error

Traceback (most recent call last):
File "webium.py", line 4, in
from webium.controls.link import Link
File "C:\Users\tiko\PycharmProjects\webium\webium.py", line 4, in
from webium.controls.link import Link
ImportError: No module named controls.link

Don't know what to do...?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.