简体   繁体   中英

Can I log in into application with selenium and them get the bearer token generated and use it in api call?Is there any way to get bearer token?

Can you login to application with selenium and then get the bearer token of that session for using it into api call? I can not login to application with login api as password is salted.. and for performing operations after login using api call, I need bearer token so can you get bearer token? Or else can we use login api with salted password generator but salted password functionality is not known!

I have tried using jsexecuter for getting bearer token

I personally would look into JMeter and making the api calls through that.

You can parametize JMeter calls and extract values from responses on JMeter to use on the next call...

Have a look at that.

Selenium doesn't do what you want it to.

You can do that using Playwright, Below is the example code.

    Public String accessToken="";
    @BeforeClass(alwaysRun = true)
    public void getAPITokenFromBrowser()
    {
        Playwright playwright = Playwright.create();
        Browser browser = playwright.chromium().launch(new BrowserType.LaunchOptions().setHeadless(false));
        Page page = browser.newPage();
        page.onRequest(request -> {
                    if (request.url().equals("www.YOUR_APP_URL"+"/API_PATH_FROM_NETWORK_TAB")) {
//if the API Call is found with above condition then spliting using Space because these days api tokens are in format "Bearer abcdsdsdsd"

String[] token = request.headers().get("authorization").split(" ");
                       this.accessToken = token[1];
                       System.out.println("AccessToken: "+accessToken);
                    }

                }
        );


        page.navigate(YOUR_URL);
        page.waitForLoadState(LoadState.LOAD);
        page.fill("UsernameXpath", "username");
        page.fill("PasswordXpath", "Password");
        page.click("LoginBtnXpath");
        page.waitForLoadState(LoadState.LOAD);
        playwright.close();
    }

Once this code block gets executed you will have your token in the variable accessToken. The only important thing here to Identify the API_PATH_FROM.NETWORK_TAB

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM