def __init__(self, ip, timeout = 0, pressDelay = 0, path = ""):
     self._process = None
     if len(path) > 0:
         self._process = subprocess.Popen(path)
     self.ROBOT_LIBRARY_LISTENER = self
     self._client = WebDriver(ip, timeout, pressDelay)
     self.markTimer()
Beispiel #2
0
 def __init__(self, ip, timeout=0, pressDelay=0, path=""):
     self._process = None
     if len(path) > 0:
         self._process = subprocess.Popen(path)
     self.ROBOT_LIBRARY_LISTENER = self
     self.locatorHandlers = {
         "attr": self._checkAttribute,
         "tag": self._checkTag,
         "text": self._checkText
     }
     self._client = WebDriver(ip, timeout, pressDelay)
     self.markTimer()
Beispiel #3
0
class RobotLibrary:

    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self, ip, timeout=0, pressDelay=0, path=""):
        self._process = None
        if len(path) > 0:
            self._process = subprocess.Popen(path)
        self.ROBOT_LIBRARY_LISTENER = self
        self._client = WebDriver(ip, timeout, pressDelay)

    def close(self):
        self._client.quiet()
        if self._process != None:
            self._process.kill()

    @keyword("Launch the channel")
    def launchTheChannel(self, channel_code):
        launch_response = self._client.send_launch_channel(channel_code)
        print(launch_response)
        self._checkResponse(launch_response)

    @keyword("Get apps")
    def getApps(self):
        apps_response = self._client.get_apps()
        self._checkResponse(apps_response)
        res = json.loads(apps_response.text)
        return res['value']

    @keyword("Verify is channel exist")
    def verifyIsChannelExist(self, apps, id):
        for app in apps:
            if app['ID'] == id:
                return True
        raise Exception("Channel doesn't exist")

    @keyword("Verify is screen loaded")
    def verifyIsScreenLoaded(self, data: object, retries=10, delay=1):
        print(data)
        while retries > 0:
            ui_layout_response = self._client.get_ui_element(data)
            if ui_layout_response.status_code != 200:
                retries -= 1
                sleep(delay)
            else:
                return True
        raise Exception("Can't find element")

    @keyword("Send key")
    def pressBtn(self, key_press: str, delay=2):
        sleep(delay)
        key_press_response = self._client.send_keypress(key_press)
        self._checkResponse(key_press_response)

    @keyword("Send word")
    def sendWord(self, word: str, delay=2):
        sleep(delay)
        for c in word:
            sleep(0.5)
            key_press_response = self._client.send_keypress(f"LIT_{c}")
            self._checkResponse(key_press_response)

    @keyword("Send keys")
    def send_button_sequence(self, sequence, delay=2):
        sleep(delay)
        key_press_response = self._client.send_sequence(sequence)
        self._checkResponse(key_press_response)

    @keyword("Get element")
    def getElement(self, data: object, delay=1):
        sleep(delay)
        ui_layout_response = self._client.get_ui_element(data)
        self._checkResponse(ui_layout_response)
        res = json.loads(ui_layout_response.text)
        return res['value']

    @keyword("Get elements")
    def getElements(self, data: object, delay=1):
        sleep(delay)
        ui_layout_response = self._client.get_ui_elements(data)
        self._checkResponse(ui_layout_response)
        res = json.loads(ui_layout_response.text)
        print(delay)
        print(len(res['value']))
        return res['value']

    @keyword("Get focused element")
    def getFocusedElement(self):
        ui_layout_response = self._client.get_active_element()
        self._checkResponse(ui_layout_response)
        res = json.loads(ui_layout_response.text)
        return res['value']

    @keyword("Verify is channel loaded")
    def verifyIsChannelLoaded(self, id, retries=10, delay=1):
        while retries > 0:
            app_response = self._client.get_current_app()
            self._checkResponse(app_response)
            res = json.loads(app_response.text)
            if res['value']['ID'] != id:
                retries -= 1
                sleep(delay)
            else:
                return True
        raise Exception("Channel isn't launched")

    @keyword("Get current channel info")
    def getCurrentChannelInfo(self):
        app_response = self._client.get_current_app()
        self._checkResponse(app_response)
        res = json.loads(app_response.text)
        channel = res['value']
        return channel

    @keyword("Get device info")
    def getDeviceInfo(self):
        response = self._client.get_device_info()
        self._checkResponse(response)
        res = json.loads(response.text)
        return res['value']

    @keyword("Get player info")
    def getPlayerInfo(self):
        response = self._client.get_player_info()
        self._checkResponse(response)
        res = json.loads(response.text)
        value = res['value']
        value['Position'] = int(self._getMsFromString(value['Position']))
        value['Duration'] = int(self._getMsFromString(value['Duration']))
        return value

    @keyword("Verify is playback started")
    def verifyIsPlaybackStarted(self, retries=10, delay=1):
        while retries > 0:
            response = self._client.get_player_info()
            res = json.loads(response.text)
            if response.status_code != 200 or res['value']['State'] != 'play':
                retries -= 1
                sleep(delay)
            else:
                return True
        raise Exception("Invalid player state")

    @keyword("Set timeout")
    def setTimeout(self, timeout: int):
        response = self._client.set_timeouts("implicit", timeout)
        self._checkResponse(response)

    @keyword("Set press delay")
    def setDelay(self, delay: int):
        response = self._client.set_timeouts("pressDelay", delay)
        self._checkResponse(response)

    @keyword("Get attribute")
    def getAttribute(self, element, attr):
        for attrObj in element['Attrs']:
            if attrObj['Name']["Local"] == attr:
                return attrObj['Value']
        raise Exception("Can't find attribute")

    def _checkResponse(self, response):
        if response.status_code == 400:
            raise Exception(response.text)
        elif response.status_code != 200:
            res = json.loads(response.text)
            raise Exception(res['value']['message'])

    def _getMsFromString(self, str):
        data = str.split(' ')
        return data[0]
Beispiel #4
0
class RobotLibrary:

    ROBOT_LIBRARY_SCOPE = 'GLOBAL'
    ROBOT_LISTENER_API_VERSION = 2

    def __init__(self, ip, timeout=0, pressDelay=0, path=""):
        self._process = None
        if len(path) > 0:
            self._process = subprocess.Popen(path)
        self.ROBOT_LIBRARY_LISTENER = self
        self.locatorHandlers = {
            "attr": self._checkAttribute,
            "tag": self._checkTag,
            "text": self._checkText
        }
        self._client = WebDriver(ip, timeout, pressDelay)
        self.markTimer()

    def close(self):
        self._client.quiet()
        if self._process != None:
            self._process.kill()

    @keyword("Mark timer")
    def markTimer(self):
        self._startTime = datetime.now()

    @keyword("Get timer")
    def getTimer(self):
        currentTime = datetime.now()
        delta = currentTime - self._startTime
        return int(delta / timedelta(milliseconds=1))

    @keyword("Side load")
    def sideLoad(self, path, user, password):
        multipart_form_data = {
            'channel': ('channel.zip', open(path, 'rb')),
            'username': (None, user),
            'password': (None, password)
        }
        response = self._client.side_load(multipart_form_data)
        self._checkResponse(response)

    @keyword("Launch the channel")
    def launchTheChannel(self, channel_code, contentId="", mediaType=""):
        launch_response = self._client.send_launch_channel(
            channel_code, contentId, mediaType)
        self._checkResponse(launch_response)

    @keyword("Get apps")
    def getApps(self):
        apps_response = self._client.get_apps()
        self._checkResponse(apps_response)
        res = json.loads(apps_response.text)
        return res['value']

    @keyword("Verify is channel exist")
    def verifyIsChannelExist(self, apps, id):
        for app in apps:
            if app['ID'] == id:
                return True
        raise Exception("Channel doesn't exist")

    @keyword("Verify is screen loaded")
    def verifyIsScreenLoaded(self, data: object, retries=10, delay=1):
        print(data)
        while retries > 0:
            ui_layout_response = self._client.get_ui_element(data)
            if ui_layout_response.status_code != 200:
                retries -= 1
                sleep(delay)
            else:
                return True
        raise Exception("Can't find element")

    @keyword("Send key")
    def pressBtn(self, key_press: str, delay=2):
        sleep(delay)
        key_press_response = self._client.send_keypress(key_press)
        self._checkResponse(key_press_response)

    @keyword("Send word")
    def sendWord(self, word: str, delay=2):
        sleep(delay)
        for c in word:
            sleep(0.5)
            key_press_response = self._client.send_keypress(
                urllib.parse.quote(f"LIT_{c}"))
            self._checkResponse(key_press_response)

    @keyword("Send keys")
    def sendButtonSequence(self, sequence, delay=2):
        sleep(delay)
        key_press_response = self._client.send_sequence(sequence)
        self._checkResponse(key_press_response)

    @keyword("Get element")
    def getElement(self, data: object, delay=1):
        sleep(delay)
        ui_layout_response = self._client.get_ui_element(data)
        self._checkResponse(ui_layout_response)
        res = json.loads(ui_layout_response.text)
        return res['value']

    @keyword("Get elements")
    def getElements(self, data: object, delay=1):
        sleep(delay)
        ui_layout_response = self._client.get_ui_elements(data)
        self._checkResponse(ui_layout_response)
        res = json.loads(ui_layout_response.text)
        return res['value']

    @keyword("Get focused element")
    def getFocusedElement(self):
        ui_layout_response = self._client.get_active_element()
        self._checkResponse(ui_layout_response)
        res = json.loads(ui_layout_response.text)
        return res['value']

    @keyword("Verify is channel loaded")
    def verifyIsChannelLoaded(self, id, retries=10, delay=1):
        while retries > 0:
            app_response = self._client.get_current_app()
            self._checkResponse(app_response)
            res = json.loads(app_response.text)
            if res['value']['ID'] != id:
                retries -= 1
                sleep(delay)
            else:
                return True
        raise Exception("Channel isn't launched")

    @keyword("Get current channel info")
    def getCurrentChannelInfo(self):
        app_response = self._client.get_current_app()
        self._checkResponse(app_response)
        res = json.loads(app_response.text)
        return res['value']

    @keyword("Get device info")
    def getDeviceInfo(self):
        response = self._client.get_device_info()
        self._checkResponse(response)
        res = json.loads(response.text)
        return res['value']

    @keyword("Get player info")
    def getPlayerInfo(self):
        response = self._client.get_player_info()
        self._checkResponse(response)
        res = json.loads(response.text)
        value = res['value']
        position = value['Position']
        duration = value['Duration']
        value['Position'] = self._getMsFromString(position)
        value['Duration'] = self._getMsFromString(duration)
        return value

    @keyword("Verify is playback started")
    def verifyIsPlaybackStarted(self, retries=10, delay=1):
        while retries > 0:
            response = self._client.get_player_info()
            res = json.loads(response.text)
            if response.status_code != 200 or res['value']['State'] != 'play':
                retries -= 1
                sleep(delay)
            else:
                return True
        raise Exception("Invalid player state")

    @keyword("Set timeout")
    def setTimeout(self, timeout: int):
        response = self._client.set_timeouts("implicit", timeout)
        self._checkResponse(response)

    @keyword("Set press delay")
    def setDelay(self, delay: int):
        response = self._client.set_timeouts("pressDelay", delay)
        self._checkResponse(response)

    @keyword("Get attribute")
    def getAttribute(self, element, attr):
        for attrObj in element['Attrs']:
            if attrObj['Name']["Local"] == attr:
                return attrObj['Value']
        raise Exception("Can't find attribute")

    @keyword("Input deep linking data")
    def inputDeepLinkingData(self, channelId, contentId, mediaType):
        launch_response = self._client.send_input_data(channelId, contentId,
                                                       mediaType)
        self._checkResponse(launch_response)

    @keyword("Get child nodes")
    def getChildNodes(self, parentNode, locators):
        childNodes = parentNode.Nodes
        result = []
        if childNodes == None:
            return result
        if locators == None:
            return result
        for node in childNodes:
            if self._isElementMatchLocators(node, locators) == True:
                result.append(node)
            result.extend(self.getChildNodes(node, locators))
        return result

    def _isElementMatchLocators(self, node, locators):
        for locator in locators:
            if hasattr(locator, 'using') == False:
                return False
            handler = self.locatorHandlers[locator.using]
            if handler == None:
                return False
            isMatch = handler(node, locator)
            if isMatch == False:
                return False
        return True

    def _checkAttribute(self, node, locator):
        if hasattr(node, 'Attrs') == False or hasattr(
                locator, 'value') == False or hasattr(locator,
                                                      'attribute') == False:
            return False
        for attr in node.Attrs:
            matchName = attr.Name.Local.lower() == locator.attribute.lower()
            matchValue = attr.Value.lower() == locator.value.lower()
            if matchName and matchValue:
                return True
        return False

    def _checkTag(self, node, locator):
        return node.XMLName.Local.lower() == locator.value.lower()

    def _checkText(self, node, locator):
        if hasattr(node, 'Attrs') == False or hasattr(locator,
                                                      'value') == False:
            return False
        for attr in node.Attrs:
            matchName = attr.Name.Local.lower() == "text"
            matchValue = attr.Value.lower() == locator.value.lower()
            if matchName and matchValue:
                return True
        return False

    def _checkResponse(self, response):
        if response.status_code == 400:
            raise Exception(response.text)
        elif response.status_code != 200:
            res = json.loads(response.text)
            raise Exception(res['value']['message'])

    def _getMsFromString(self, strWithMs):
        ms = 0
        if type(strWithMs) == str:
            searchRes = re.search(r'\d+', strWithMs)
            if searchRes != None:
                ms = int(searchRes.group())
        return ms
 def setUp(self):
     with patch('Library.webDriver.requests.post') as mock_post:
         mock_post.return_value.status_code = 200
         mock_post.return_value.text = json.dumps(resp)
         self._webDriver = WebDriver("1.1.0.127", 5000, 2000)
class BasicTests(unittest.TestCase):
    def setUp(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(resp)
            self._webDriver = WebDriver("1.1.0.127", 5000, 2000)

    def test_send_keypress(self):
        with patch('Library.webDriver.requests.post') as mock_get:
            mock_get.return_value.status_code = 200
            mock_get.return_value.text = json.dumps(respWithEmptyValue)
            response = self._webDriver.send_keypress("select")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)

    def test_send_launch_channel(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithEmptyValue)
            response = self._webDriver.send_launch_channel("dev", "", "")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)

    def test_send_install_channel(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithEmptyValue)
            response = self._webDriver.send_install_channel("1111")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)

    def test_get_device_info(self):
        with patch('Library.webDriver.requests.get') as mock_get:
            mock_get.return_value.status_code = 200
            mock_get.return_value.text = json.dumps(respWithDeviceInfo)
            response = self._webDriver.get_device_info()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithDeviceInfo)

    def test_send_sequence(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithEmptyValue)
            response = self._webDriver.send_sequence(["up", "left"])
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)

    def test_get_apps(self):
        with patch('Library.webDriver.requests.get') as mock_get:
            mock_get.return_value.status_code = 200
            mock_get.return_value.text = json.dumps(respWithApps)
            response = self._webDriver.get_apps()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithApps)

    def test_get_player(self):
        with patch('Library.webDriver.requests.get') as mock_get:
            mock_get.return_value.status_code = 200
            mock_get.return_value.text = json.dumps(respWithPlayerInfo)
            response = self._webDriver.get_player_info()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithPlayerInfo)

    def test_get_current_app(self):
        with patch('Library.webDriver.requests.get') as mock_get:
            mock_get.return_value.status_code = 200
            mock_get.return_value.text = json.dumps(respWithApp)
            response = self._webDriver.get_current_app()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithApp)

    def test_quiet(self):
        with patch('Library.webDriver.requests.delete') as mock_delete:
            mock_delete.return_value.status_code = 200
            mock_delete.return_value.text = json.dumps(respWithEmptyValue)
            response = self._webDriver.quiet()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)

    def test_get_ui_element(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithElement)
            response = self._webDriver.get_ui_element([{
                "using": "text",
                "value": "text"
            }])
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithElement)

    def test_get_ui_elements(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithElements)
            response = self._webDriver.get_ui_elements([{
                "using": "text",
                "value": "text"
            }])
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithElements)

    def test_get_active_element(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithElement)
            response = self._webDriver.get_active_element()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithElement)

    def test_set_timeouts(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithEmptyValue)
            response = self._webDriver.set_timeouts("implicit", "2000")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)

    def test_get_screen_source(self):
        with patch('Library.webDriver.requests.get') as mock_get:
            mock_get.return_value.status_code = 200
            mock_get.return_value.text = json.dumps(respWithSource)
            response = self._webDriver.get_screen_source()
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithSource)

    def test_send_input_data(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithEmptyValue)
            response = self._webDriver.send_input_data("dev", "12", "movie")
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)

    def test_side_load(self):
        with patch('Library.webDriver.requests.post') as mock_post:
            mock_post.return_value.status_code = 200
            mock_post.return_value.text = json.dumps(respWithEmptyValue)
            multipart_form_data = {
                'username': (None, "user"),
                'password': (None, "pass")
            }
            response = self._webDriver.side_load(multipart_form_data)
        self.assertEqual(response.status_code, 200)
        self.assertEqual(json.loads(response.text), respWithEmptyValue)