简体   繁体   中英

I want to get orderID from API response using selenium python

I want to get the order ID from the API response. When I click on the Create Order button it will send a POST API request and return the unique ID that I want.

This is my order creation code.

from datetime import date
import time
from seleniumwire import webdriver
from openpyxl import load_workbook
from Locators.PracticeLocators import PracticeLocators
from pageObjects.LoginPage import LoginScreen

today = date.today()
currentDate = today.strftime("%m/%d/%Y")
FilePath = "C:/Users/Administrator/PycharmProject/LegrandePython/TestData/Data.xlsx"
datafile = load_workbook(FilePath)
testData = datafile['Test Data']
loginData = datafile["Login Credentials"]
scriptData = datafile["Script Data"]

driver = webdriver.Chrome(executable_path="C:/Users/Administrator/Downloads/chromedriver.exe")
driver.maximize_window()

driver.scopes = [
    '.*https://ibis-dev.droicelabs.us/api/dispenser/orders/.*'
]

driver.get(loginData.cell(4, 2).value)
driver.implicitly_wait(5)
login = LoginScreen(driver)
login.SetUsername(loginData.cell(4, 3).value)
login.SetPassword(loginData.cell(4, 4).value)
login.SignIn()
driver.implicitly_wait(20)
driver.find_element_by_class_name(PracticeLocators.Add_RX).click()
PatientSearch = driver.find_element_by_xpath(PracticeLocators.Patient_search_textbox)
PatientSearch.click()
PatientSearch.send_keys(testData.cell(2, 1).value)
driver.find_element_by_xpath("(//*[text()='" + testData.cell(2, 2).value + "'])[1]").click()
DoctorSearch = driver.find_element_by_xpath(PracticeLocators.doctor_search_textbox)
DoctorSearch.click()
time.sleep(1)
DoctorSearch.send_keys(scriptData.cell(2, 8).value)
time.sleep(1)
driver.find_element_by_xpath(
    "(//*[text()='" + scriptData.cell(2, 8).value + " " + "Practice'])[2]").click()
driver.find_element_by_xpath(PracticeLocators.NextButton).click()
driver.find_element_by_xpath(PracticeLocators.CreateOnetimeRXButton).click()
driver.find_element_by_name(PracticeLocators.OnetimeSearchMedicine).send_keys(scriptData.cell(2, 1).value)
time.sleep(2)
driver.find_element_by_xpath("//*[text()='" + scriptData.cell(2, 1).value + "']").click()
driver.find_element_by_xpath(PracticeLocators.AddButton).click()
driver.find_element_by_xpath(PracticeLocators.ProductQuantity).click()
time.sleep(1)
driver.find_element_by_xpath(PracticeLocators.Quantity).click()
driver.find_element_by_xpath(PracticeLocators.ProductRefilles).click()
time.sleep(1)
driver.find_element_by_xpath(PracticeLocators.Quantity).click()
time.sleep(2)
driver.find_element_by_xpath(PracticeLocators.DAWCheckbox).click()
time.sleep(2)
instruction = driver.find_element_by_xpath(PracticeLocators.productInstruction)
instruction.click()
instruction.send_keys(testData.cell(2, 3).value)
driver.find_element_by_xpath(PracticeLocators.allergiesButton).click()
allergies = driver.find_element_by_xpath(PracticeLocators.allergiesTextbox)
allergies.clear()
allergies.send_keys(testData.cell(2, 4).value)
driver.find_element_by_xpath(PracticeLocators.doneButton).click()
driver.find_element_by_xpath(PracticeLocators.addDropchartButton).click()
time.sleep(2)
element = driver.find_element_by_xpath(PracticeLocators.selectDocuments)
driver.execute_script("arguments[0].click()", element)
driver.find_element_by_xpath(PracticeLocators.selectButton).click()
driver.find_element_by_xpath(PracticeLocators.skipPayment).click()
driver.find_element_by_xpath(PracticeLocators.surgeryDate).send_keys(currentDate)
createOrderButton = driver.find_element_by_xpath(PracticeLocators.submit_CreateOrderButton)
driver.execute_script("arguments[0].click()", createOrderButton)
for request in driver.requests:
    if request.response:
        print(
            request.url,
            request.response.status_code,
            request.response.headers['Content-Type'])
time.sleep(7)

When self.driver.execute_script("arguments[0].click()", createOrderButton) this line execute it will send the POST API https://ibis-dev.droicelabs.us/api/dispenser/orders/ and return unique order id that I want.

I have give scope but it will return many API call

driver.scopes = [
    '.*https://ibis-dev.droicelabs.us/api/dispenser/orders/.*'
]

https://ibis-dev.droicelabs.us/api/dispenser/orders/ this API will call when I click on the create order button and return order id. It will not show the API that I have used in a scope. And how to get the response of that API which I have given in scope. I am new to selenium python and I have done it in cypress. but don't know how to do it using selenium python.

This is how to capture HTTP requests using Selenium:

1 Install package

pip install selenium-wire 

2 Use driver.requests to get some data

from seleniumwire import webdriver

driver = webdriver.Chrome()

# do you actions with driver

for request in driver.requests:  
    if request.response:  
        print(  
            request.url,  
            request.response.status_code,  
            request.response.headers['Content-Type'])  

3 You might want to limit requests capture to some specific hosts

https://pypi.org/project/selenium-wire/#limiting-request-capture

driver.scopes = [
    '.*ibis-dev.droicelabs.us/api/dispenser/orders/.*'
]

4 How to read response body

import json

...

    for request in driver.requests:
        if request.response:
            data = json.loads(request.response.body)
            print(data['foo'])

Answering this question - get order id

driver.requests[-1] - getting the latest request

...
createOrderButton = driver.find_element_by_xpath(PracticeLocators.submit_CreateOrderButton)
driver.execute_script("arguments[0].click()", createOrderButton)
time.sleep(2)

order_id = json.loads(driver.requests[-1].response.body)["_id"]

Limitations

Selenium Wire will currently work with tests that run on the same machine as the browser. A distributed setup using Selenium Grid is not yet supported.

Sites that use NTLM authentication (Windows authentication) cannot currently be tested with Selenium Wire. NTLM authentication is not supported.


Reference

https://pypi.org/project/selenium-wire/

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