Subversion Repositories basico

Rev

Rev 184 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
35 t00mlabs 1
#!/usr/bin/python
2
# -*- coding: utf-8 -*-
3
# File: driver.py
4
# Author: Tomás Vírseda
5
# License: GPL v3
6
# Description: Selenium Driver service
7
 
8
import os
9
import selenium
10
from selenium import webdriver
11
from selenium.webdriver.support.wait import WebDriverWait
12
from selenium import webdriver
13
from selenium.webdriver.common.keys import Keys
14
from selenium.common.exceptions import WebDriverException
15
from selenium.common.exceptions import NoSuchElementException
16
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
17
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
143 t00mlabs 18
from selenium.webdriver.firefox.options import Options
35 t00mlabs 19
 
233 t00m 20
from basico.core.service import Service
35 t00mlabs 21
 
22
GECKODRIVER_URL = "https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz"
23
 
172 t00m 24
 
35 t00mlabs 25
class SeleniumDriver(Service):
26
    def open(self):
27
        '''
28
        In order to have selenium working with Firefox and be able to
29
        get SAP Notes from launchpad.support.sap.com you must:
73 t00mlabs 30
        1. Use a browser certificate (SAP Passport) in order to avoid
31
           renewed logons.
35 t00mlabs 32
           You can apply for it at:
33
           https://support.sap.com/support-programs-services/about/getting-started/passport.html
34
        2. Get certificate and import it into Firefox.
35
           Open menu -> Preferences -> Advanced -> View Certificates
36
           -> Your Certificates -> Import
73 t00mlabs 37
        3. Trust this certificate (auto select)
38
        4. Check it. Visit some SAP Note url in Launchpad.
39
           No credentials will be asked.
40
           Launchpad must load target page successfully.
35 t00mlabs 41
        '''
42
 
180 t00m 43
        utils = self.get_service('Utils')
44
        options = Options()
45
        # ~ Uncomment this line to set Firefox to headless
184 t00m 46
        options.add_argument('--headless')
180 t00m 47
        FIREFOX_PROFILE_DIR = utils.get_firefox_profile_dir()
48
        FIREFOX_PROFILE = webdriver.FirefoxProfile(FIREFOX_PROFILE_DIR)
49
        driver = webdriver.Firefox(firefox_profile=FIREFOX_PROFILE, firefox_options=options)
50
        self.debug("Webdriver initialited")
51
        return driver
35 t00mlabs 52
 
53
 
180 t00m 54
    def close(self, driver):
55
        driver.quit()
56
        self.debug("Webdriver closed")
57
        driver = None
35 t00mlabs 58
 
59
 
60
 
171 t00m 61
    def load(self, driver, URL):
180 t00m 62
        driver.get(URL)
35 t00mlabs 63
        return driver
64
 
65
 
73 t00mlabs 66
    def check(self):
67
        """
68
        Check gecko webdriver
69
        You must install geckodriver. It is mandatory
70
        Yo can download it from:
71
        https://github.com/mozilla/geckodriver/
72
        Then, extract the binary and copy it to somewhere in your $PATH.
73
        If OS is Linux: /usr/local/bin/geckodriver
74
        If OS is Windows: C:\Windows\System32 or elsewhere.
75
 
76
        Basico will try to do it for you.
77
        """
78
        utils = self.get_service('Utils')
79
 
80
        # First, add BASICO OPT Path to $PATH
81
        GECKO_INSTALL_DIR = self.get_var('DRIVERS', 'local')
82
        os.environ["PATH"] += os.pathsep + GECKO_INSTALL_DIR
83
        # Then, look for Geckodriver
84
        GECKODRIVER = utils.which('geckodriver')
85
 
86
        if not GECKODRIVER:
133 t00mlabs 87
            self.debug("Attempting to download Gecko driver and install it.")
73 t00mlabs 88
            utils.install_geckodriver()
89
 
90
        GECKODRIVER = utils.which('geckodriver')
91
        if GECKODRIVER is None:
92
            self.log.warning("Gecko driver not found.")
93
            return False
94
        else:
133 t00mlabs 95
            self.debug("Gecko Webdriver found in: %s" % GECKODRIVER)
73 t00mlabs 96
            return True
97
 
98