python - self.driver.session_id provides incorrect Sauce Labs Session ID - Stack Overflow

I'm trying to get the session_id to post a message to Telegram after the test is done so that I ca

I'm trying to get the session_id to post a message to Telegram after the test is done so that I can simply click the link and view the test run report.

I am running a single test where I attempt to retrieve the real session_id.

Here is my code:

    def test_sauce_link(self):
        main_page_1 = MainPage(self.driver)
        session_id = self.driver.session_id
        clean_session_id = re.sub(r"-", "", session_id)  
        report_url = f"/{clean_session_id}"
        TelegramReport.send_tg(f"{report_url}")

The resulting ID from the run log: Message sent to Telegram:

Meanwhile, the actual link from Sauce Labs is:

They are different, and the link from the test does not even work—it returns a 404 error when I try to open it.

What am I doing wrong here? Please help.

my Base class with caps:

class BaseTest(unittest.TestCase):
    SAUCE_URL = ":443/wd/hub"

    def setUp(self):
        """Set up Appium driver for each test case"""
        platform = os.getenv("TEST_PLATFORM", "android").lower()
        app_filename = ""

        if not app_filename:
            raise ValueError("TEST_APP_FILENAME not provided!")

        if platform == "android":
            caps = UiAutomator2Options()
            caps.set_capability("platformName", "Android")
            caps.set_capability("appium:platformVersion", "15")
            caps.set_capability("appium:deviceName", "Google Pixel 9")
            caps.set_capability("appium:automationName", "UiAutomator2")
            caps.set_capability("appium:app", f"storage:filename={app_filename}")
            caps.set_capability("autoGrantPermissions", True)
            caps.set_capability("hideKeyboard", True)
            caps.set_capability("autoHideKeyboard", True)

        else:
            raise ValueError(f"Unsupported platform: {platform}")

        # Sauce Labs options
        sauce_options = {
            "username": os.getenv("SAUCE_USERNAME", "USERNAME"),
            "accessKey": os.getenv("SAUCE_ACCESS_KEY", "SAUCE_ACCESS_KEY"),
            "build": f"appium-build-{platform.upper()}",
            "name": self.__class__.__name__, 
            "deviceOrientation": "PORTRAIT",
            "appiumVersion": "2.0.0"
        }
        caps.set_capability("sauce:options", sauce_options)

        
        self.driver = webdriver.Remote(self.SAUCE_URL, options=caps)
        self.wait = WebDriverWait(self.driver, 10)

    def tearDown(self):
        """Close driver and update job status in Sauce Labs"""
        if hasattr(self, "driver") and self.driver:
            job_status = "passed" if sys.exc_info()[0] is None else "failed"
            try:
                self.driver.execute_script(f"sauce:job-result={job_status}")
            except Exception as e:
                print(f"Failed to update Sauce Labs job status: {e}")

            try:
                self.driver.quit()
            except Exception as e:
                print(f"Failed to quit the driver: {e}")

I'm trying to get the session_id to post a message to Telegram after the test is done so that I can simply click the link and view the test run report.

I am running a single test where I attempt to retrieve the real session_id.

Here is my code:

    def test_sauce_link(self):
        main_page_1 = MainPage(self.driver)
        session_id = self.driver.session_id
        clean_session_id = re.sub(r"-", "", session_id)  
        report_url = f"https://app.eu-central-1.saucelabs/tests/{clean_session_id}"
        TelegramReport.send_tg(f"{report_url}")

The resulting ID from the run log: Message sent to Telegram:

https://app.eu-central-1.saucelabs/tests/c0ae13588ae14629bb18b2e237e42ebe

Meanwhile, the actual link from Sauce Labs is:

https://app.eu-central-1.saucelabs/tests/50f2c409f87c471b973a24ed389562d2

They are different, and the link from the test does not even work—it returns a 404 error when I try to open it.

What am I doing wrong here? Please help.

my Base class with caps:

class BaseTest(unittest.TestCase):
    SAUCE_URL = "https://ondemand.eu-central-1.saucelabs:443/wd/hub"

    def setUp(self):
        """Set up Appium driver for each test case"""
        platform = os.getenv("TEST_PLATFORM", "android").lower()
        app_filename = ""

        if not app_filename:
            raise ValueError("TEST_APP_FILENAME not provided!")

        if platform == "android":
            caps = UiAutomator2Options()
            caps.set_capability("platformName", "Android")
            caps.set_capability("appium:platformVersion", "15")
            caps.set_capability("appium:deviceName", "Google Pixel 9")
            caps.set_capability("appium:automationName", "UiAutomator2")
            caps.set_capability("appium:app", f"storage:filename={app_filename}")
            caps.set_capability("autoGrantPermissions", True)
            caps.set_capability("hideKeyboard", True)
            caps.set_capability("autoHideKeyboard", True)

        else:
            raise ValueError(f"Unsupported platform: {platform}")

        # Sauce Labs options
        sauce_options = {
            "username": os.getenv("SAUCE_USERNAME", "USERNAME"),
            "accessKey": os.getenv("SAUCE_ACCESS_KEY", "SAUCE_ACCESS_KEY"),
            "build": f"appium-build-{platform.upper()}",
            "name": self.__class__.__name__, 
            "deviceOrientation": "PORTRAIT",
            "appiumVersion": "2.0.0"
        }
        caps.set_capability("sauce:options", sauce_options)

        
        self.driver = webdriver.Remote(self.SAUCE_URL, options=caps)
        self.wait = WebDriverWait(self.driver, 10)

    def tearDown(self):
        """Close driver and update job status in Sauce Labs"""
        if hasattr(self, "driver") and self.driver:
            job_status = "passed" if sys.exc_info()[0] is None else "failed"
            try:
                self.driver.execute_script(f"sauce:job-result={job_status}")
            except Exception as e:
                print(f"Failed to update Sauce Labs job status: {e}")

            try:
                self.driver.quit()
            except Exception as e:
                print(f"Failed to quit the driver: {e}")
Share Improve this question edited Mar 21 at 13:39 Z T M N asked Mar 21 at 8:57 Z T M NZ T M N 211 silver badge4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

For anyone wonder, they answered me with this:

test_report_url = self.driver.capabilities.get("testobject_test_report_url")
TelegramReport.send_tg(f"{test_report_url}")

and finally it works!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744365262a4570670.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信