Ask a Question

Selenium Python setting test to pass

jadelaird
New Member

Selenium Python setting test to pass

Just starting out with selenium using python using the example on cross browser testing. 

 

Im only doing a simple script to start but its not setting the status to pass although I have this 

 

self.test_result = 'pass'

 

full script 

 

 

import unittest
from selenium import webdriver
import requests

class RAPLogin(unittest.TestCase):
def setUp(self):

self.username = "jade.laird@tharstern.com"
self.authkey = "u289162f9a08ba06"

self.api_session = requests.Session()
self.api_session.auth = (self.username,self.authkey)
self.test_result = None

caps = {}
caps['name'] = 'RAP - Login'
caps['browserName'] = 'Chrome'
caps['version'] = '60x64'
caps['platform'] = 'Windows 10'
caps['screenResolution'] = '1366x768'
caps['record_video'] = 'true'

self.driver = webdriver.Remote(
desired_capabilities=caps,
command_executor="http://%s:%s@hub.crossbrowsertesting.com:80/wd/hub"%(self.username,self.authkey)
)

self.driver.implicitly_wait(20)

def test_CBT(self):
self.driver.get('http://localhost/RAP/Auth/Login?ReturnUrl=%2FRAP')
self.assertEqual("Login Page - Remote Access Portal", self.driver.title)
self.test_result = 'pass'
self.driver.quit()

if __name__ == '__main__':
unittest.main()

 

1 REPLY 1
Lago
Staff

Try to warp you script in a try/except so that you can set pass/fail at the end.

Check out this link to add the try execpt to your preferred scripting language:
https://support.smartbear.com/crossbrowsertesting/docs/automated-testing/index.html

example (in python):

try:
            # load the page url
            print('Loading Url')
            self.driver.get('http://crossbrowsertesting.github.io/selenium_example_page.html')

            # maximize the window - DESKTOPS ONLY
            #print('Maximizing window')
            #self.driver.maximize_window()
            
            #check the title
            print('Checking title')
            self.assertEqual("Selenium Test Example Page", self.driver.title)

            # if we are still in the try block after all of our assertions that 
            # means our test has had no failures, so we set the status to "pass"
            self.test_result = 'pass'

        except AssertionError as e:

            # if any assertions are false, we take a snapshot of the screen, log 
            # the error message, and set the score to "during tearDown()".

            snapshot_hash = self.api_session.post('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots').json()['hash']
            self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id + '/snapshots/' + snapshot_hash,
                data={'description':"AssertionError: " + str(e)})
            self.test_result = 'fail'
            raise

    def tearDown(self):
        print("Done with session %s" % self.driver.session_id)
        self.driver.quit()
        # Here we make the api call to set the test's score.
        # Pass it it passes, fail if an assertion fails, unset if the test didn't finish
        if self.test_result is not None:
            self.api_session.put('https://crossbrowsertesting.com/api/v3/selenium/' + self.driver.session_id, 
                data={'action':'set_score', 'score':self.test_result})


if __name__ == '__main__':
    unittest.main()

 

cancel
Showing results for 
Search instead for 
Did you mean: