2

I want to access an object attribute variable in Robot Framework tests and then validate the values of these attributes.

I have the following Python class:

class TestingClass(object):


    def __init__(self, client_id=None, redirect_uri=None, state=None):
        self.client_id = client_id
        self.uri = uri
        self.state = state



    def url_authorize(self):
        self.oauthsession = OAuth2Session(client_id=self.client_id, uri=self.uri, state=self.state)
        self.authorize_endpoint_url = self.oauthsession.authorization_url(
            url="http://localhost:8080/authorize")[0]
        return self.authorize_endpoint_url


    def authorize(self):
        request = requests.get("http://localhost:8080")
        self.status_code = request.status_code
        self.content = request.content
        self.json = request.json

 

I want to be able to grab any one of the attributes created in the Authorize (authorize method) and validate it. So I want to do something like this:

* Variables
${CLIENT_ID}                        to
${URI}                              http://127.0.0.1:8080/callback
${STATE}                            random

* Settings
Documentation                       Tests Suite
Library                             Utilities.TestingClass         ${CLIENT_ID}       ${URI}
...                                                                           ${STATE}       

*Keywords

*Test Cases
Authorize Test 1
    [Documentation]                 Test that the user is redirected to the authorize 
    ...                             endpoint
    ...                             when accessing the OAuth Client
    Url Authorize
    Authorize
    
    Should Be Equal As Integers     Utilities.TestingClass.status_code     200

However this gives: Utilities.TestingClass.status_code != 200 which is no surprise to me.

How do I grab this attribute to compare it? Would I need to maybe return all the attributes made from the authroize() method in a list/dictionary/some sort of array and then access by indexing? Or is there a more straight forward way to do this with Robot Framework?

1

1 Answer 1

3

You have the following two choices, both will involve the usage of extended variable syntax. I would prefer the second option.

  1. You can use the Get Library Instance keyword, to get the library instance in the test. The you can access its member variables using the extended variable syntax. Here is an example based on your code, I just replaced the return values with constants.

    class TestingClass(object):
    
        def __init__(self, client_id=None, redirect_uri=None, state=None):
            self.client_id = client_id
            self.uri = redirect_uri
            self.state = state
    
        def url_authorize(self):
            self.oauthsession = None
            self.authorize_endpoint_url = "http://localhost:8080/authorize"
            return self.authorize_endpoint_url
    
        def authorize(self):
            self.status_code = 200
            self.content = 'content'
            self.json = { "A" : ["StringA1",  "StringA2"], "B": ["StringB1","StringB2"]}
    

    *** Settings ***
    Library    Utilities.TestingClass    to    http://127.0.0.1:8080/callback    random
    
    *** Test Cases ***
    Test
        Url Authorize
        Authorize
        ${TestingClass}=    Get Library Instance    Utilities.TestingClass
        Log    ${TestingClass.status_code}
        Log    ${TestingClass.content}
        Log    ${TestingClass.json}
    
  2. The other option is to modify the authorize method, so it will return what requests.get("http://localhost:8080") returns. Then you could access the status code, content and JSON in the same way as above, using the extended variable syntax.

    class DummyRequestReturnValue():
        def __init__(self):
            self.status_code = 200
            self.content = 'content'
            self.json = { "A" : ["StringA1",  "StringA2"], "B": ["StringB1","StringB2"]}
    
    
    class TestingClass(object):
    
        def __init__(self, client_id=None, redirect_uri=None, state=None):
            self.client_id = client_id
            self.uri = redirect_uri
            self.state = state
    
        def url_authorize(self):
            self.oauthsession = None
            self.authorize_endpoint_url = "http://localhost:8080/authorize"
            return self.authorize_endpoint_url
    
        def authorize(self):
            request = DummyRequestReturnValue() 
            return request
    

    *** Settings ***
    Library    Utilities.TestingClass    to    http://127.0.0.1:8080/callback    random
    
    *** Test Cases ***
    Test
        Url Authorize
        ${response}=    Authorize
        Log    ${response.status_code}
        Log    ${response.content}
        Log    ${response.json}
    
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.