Subversion Repositories basico

Rev

Rev 73 | 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
18
 
19
from .service import Service
20
 
21
GECKODRIVER_URL = "https://github.com/mozilla/geckodriver/releases/download/v0.15.0/geckodriver-v0.15.0-linux64.tar.gz"
22
 
23
class SeleniumDriver(Service):
24
    def initialize(self):
25
        self.driver = None
26
 
27
 
28
    def open(self):
29
        '''
30
        In order to have selenium working with Firefox and be able to
31
        get SAP Notes from launchpad.support.sap.com you must:
73 t00mlabs 32
        1. Use a browser certificate (SAP Passport) in order to avoid
33
           renewed logons.
35 t00mlabs 34
           You can apply for it at:
35
           https://support.sap.com/support-programs-services/about/getting-started/passport.html
36
        2. Get certificate and import it into Firefox.
37
           Open menu -> Preferences -> Advanced -> View Certificates
38
           -> Your Certificates -> Import
73 t00mlabs 39
        3. Trust this certificate (auto select)
40
        4. Check it. Visit some SAP Note url in Launchpad.
41
           No credentials will be asked.
42
           Launchpad must load target page successfully.
35 t00mlabs 43
        '''
44
 
45
        if self.driver is None:
46
            utils = self.get_service('Utils')
47
            FIREFOX_PROFILE_DIR = utils.get_firefox_profile_dir()
48
            FIREFOX_PROFILE = webdriver.FirefoxProfile(FIREFOX_PROFILE_DIR)
49
            driver = webdriver.Firefox(FIREFOX_PROFILE)
50
            self.driver = driver
51
            self.log.debug("Webdriver initialited")
52
 
53
        return self.driver
54
 
55
 
56
    def close(self):
57
        try:
58
            self.driver.quit()
38 t00mlabs 59
            self.log.debug("\tWebdriver closed")
35 t00mlabs 60
        except:
61
            self.log.debug("Webdriver already closed")
62
            pass
63
 
64
        self.driver = None
65
 
66
 
67
 
68
    def load(self, URL):
69
        driver = self.open()
70
        driver.get(URL)
71
        return driver
72
 
73
 
73 t00mlabs 74
    def check(self):
75
        """
76
        Check gecko webdriver
77
        You must install geckodriver. It is mandatory
78
        Yo can download it from:
79
        https://github.com/mozilla/geckodriver/
80
        Then, extract the binary and copy it to somewhere in your $PATH.
81
        If OS is Linux: /usr/local/bin/geckodriver
82
        If OS is Windows: C:\Windows\System32 or elsewhere.
83
 
84
        Basico will try to do it for you.
85
        """
86
        utils = self.get_service('Utils')
87
 
88
        # First, add BASICO OPT Path to $PATH
89
        GECKO_INSTALL_DIR = self.get_var('DRIVERS', 'local')
90
        os.environ["PATH"] += os.pathsep + GECKO_INSTALL_DIR
91
        # Then, look for Geckodriver
92
        GECKODRIVER = utils.which('geckodriver')
93
 
94
        if not GECKODRIVER:
95
            self.log.debug("Attempting to download and install it.")
96
            utils.install_geckodriver()
97
 
98
        GECKODRIVER = utils.which('geckodriver')
99
        if GECKODRIVER is None:
100
            self.log.warning("Gecko driver not found.")
101
            return False
102
        else:
103
            self.log.debug("Gecko Webdriver found in: %s" % GECKODRIVER)
104
            return True
105
 
106
 
35 t00mlabs 107
    def run(self):
108
        pass
109
 
110
 
111
    def quit(self):
112
        self.close()
113
 
114
 
115
    def end(self):
116
        self.close()
117
 
118