<commit_before># xpathwebdriver

[![Codeship Status for joaduo/xpathwebdriver](https://app.codeship.com/projects/a77b1220-ecf1-0137-98b2-4e2936dc9ea3/status?branch=master)](https://app.codeship.com/projects/374749)

A python wrapper for interacting with Selenium through XPath and CSS selectors.
The main difference is that you can use use XPaths like:

```
//div/text()
```

Which will return you a string, something webdriver API makes more complicated.

## Quick install

```
pip install xpathwebdriver
```

1. Install xpathwebdriver using pip.
2. Install google chrome.
3. Download chromedriver for your chrome version and install it in your path.
   https://chromedriver.chromium.org/downloads
4. That generally should work on a modern Linux System (not tested but should also work on other oses).
   Try example below
5. For image comparison `pip install Pillow` and install `findimagedupes` and `imagemagick` packages for your OS

Check "Installing Selenium" section for other browsers and details.

## Example

```python
from xpathwebdriver.simple_xpath_browser import SimpleXpathBrowser

browser = SimpleXpathBrowser()
browser.get_url('https://duckduckgo.com/')
browser.fill(".//*[@id='search_form_input_homepage']", 'xpathwebdriver\n')
# Using xpath that returns text
for idx, t in enumerate(browser.select_xpath('//div/text()')):
    print(idx, t)
# Using css selector which returns elements
for idx, elem in enumerate(browser.select_css('.result__title')):
    print(idx, elem.text)
```

## IPython interactive shell

For a faster development cycles you can run an interactive shell which will let access the browser API. First install ipython `pip install ipython` (it's not mandatory to keep requirements small in non dev environment)

Then you can run the `xpathshell` in your terminal. You should see something like:

```
$ xpathshell
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.

XpathBrowser in 'b' or 'browser' variables
 Current url: data:,
In [1]: b.get('github.com/joaduo/xpathwebdriver/')
INFO 05:53:35:  Current url: https://github.com/joaduo/xpathwebdriver/

```

Pass the url in the command arguments too. Eg: `xpathshell duckduckgo.com`

On IPython you can enter `browser.select_xpath?` to get documentation on `select_xpath` method.
This way you can access API docs.

More `XpathBrowser` details at:

* https://github.com/joaduo/xpathwebdriver/blob/master/BrowserAPI.md
* https://github.com/joaduo/xpathwebdriver/blob/master/xpathwebdriver/xpath_browser.py
* https://github.com/joaduo/xpathwebdriver/blob/master/xpathwebdriver_tests/test_XpathBrowser.py

### Using unittest library


```python
import unittest
from xpathwebdriver.simple_xpath_browser import SimpleXpathBrowser

class SearchEnginesDemo(unittest.TestCase):
    def setUp(self):
        # Get Xpath browser
        self.browser = SimpleXpathBrowser()

    def tearDown(self):
        # Make sure we quit those webdrivers created in this specific "level of life"
        del self.browser

    def test_duckduckgo(self):
        # Load a local page for the demo
        self.browser.get_url('https://duckduckgo.com/')<commit_msg>Improve README<commit_after># xpathwebdriver

[![Codeship Status for joaduo/xpathwebdriver](https://app.codeship.com/projects/a77b1220-ecf1-0137-98b2-4e2936dc9ea3/status?branch=master)](https://app.codeship.com/projects/374749)

A python wrapper for interacting with Selenium through XPath and CSS selectors.
The main difference is that you can use use XPaths like:

```
//div/text()
```

Which will return you a string, something webdriver API makes more complicated.
Means you can write all your tests based on XPath without having to do heavy python manipulation
of the resulting objects.

Also adds:

- Interactive shell for testing XPath manually and easily
- Virtual display management
- Multiple browser management
- Browser life management (wether to keep the browser open or kill it on exit)
- Useful settings for local and remote (headless) testing
  - Also supports environment variables as settings
- Screenshots comparison and diff management

## Quick install

```
pip install xpathwebdriver
```

1. Install xpathwebdriver using pip.
2. Install google chrome.
3. Download chromedriver for your chrome version and install it in your path.
   https://chromedriver.chromium.org/downloads
4. That generally should work on a modern Linux System (not tested but should also work on other oses).
   Try example below
5. For image comparison install `pip install Pillow`,  `findimagedupes` and `imagemagick` packages for your OS. (they are not automatically installed to keep basic requirments low)
   On ubuntu: `sudo apt install imagemagick findimagedupes`
6. For interactive shell install `pip install ipython`

Check "Installing Selenium" section for other browsers and details.

## Example

```python
from xpathwebdriver.simple_xpath_browser import SimpleXpathBrowser

browser = SimpleXpathBrowser()
browser.get_url('https://duckduckgo.com/')
browser.fill(".//*[@id='search_form_input_homepage']", 'xpathwebdriver\n')
# Using xpath that returns text
for idx, t in enumerate(browser.select_xpath('//div/text()')):
    print(idx, t)
# Using css selector which returns elements
for idx, elem in enumerate(browser.select_css('.result__title')):
    print(idx, elem.text)
```

## IPython interactive shell

For a faster development and debugging cycles you can run an interactive shell which will let access the browser. First install ipython `pip install ipython` (not automatically installed to keep basic requirements low)

Then you can run the `xpathshell` in your terminal. You should see something like:

```
$ xpathshell
Python 3.7.5rc1 (default, Oct  8 2019, 16:47:45)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.9.0 -- An enhanced Interactive Python. Type '?' for help.

XpathBrowser in 'b' or 'browser' variables
 Current url: data:,
In [1]: b.get('github.com/joaduo/xpathwebdriver/')
INFO 05:53:35:  Current url: https://github.com/joaduo/xpathwebdriver/

```

Or pass the url in the command arguments. Eg: `xpathshell github.com/joaduo/xpathwebdriver/`

Inside IPython you can enter `browser.select_xpath?` to get documentation and can access API docs.

More `XpathBrowser` details at:

* https://github.com/joaduo/xpathwebdriver/blob/master/BrowserAPI.md
* https://github.com/joaduo/xpathwebdriver/blob/master/xpathwebdriver/xpath_browser.py
* https://github.com/joaduo/xpathwebdriver/blob/master/xpathwebdriver_tests/test_XpathBrowser.py

### Using unittest library


```python
import unittest
from xpathwebdriver.simple_xpath_browser import SimpleXpathBrowser

class SearchEnginesDemo(unittest.TestCase):
    def setUp(self):
        # Get Xpath browser
        self.browser = SimpleXpathBrowser()

    def tearDown(self):
        # Make sure we quit those webdrivers created in this specific "level of life"
        del self.browser

    def test_duckduckgo(self):
        # Load a local page for the demo
        self.browser.get_url('https://duckduckgo.com/')
{'fraction_non_alphanumeric': 0.09324399776661083, 'fraction_numerical': 0.01605248464544947, 'mean_word_length': 4.6328616352201255, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 18, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1282704', 'n_tokens_mistral': 2253, 'n_tokens_neox': 2125, 'n_words': 747}