Search code examples
python-3.xseleniumxpathautomated-testsparameterized-unit-test

How to parameterize xpath and parse it as a text?


I have a python file, Base.Py

def provideElectricJobDetails(self):
        self.ui.driver.find_element_by_name("Title").send_keys("Test")
        self.ui.driver.find_element_by_xpath("//span[contains(text(),'Select AOR...')]").click()
        self.ui.sleep(2)
        self.ui.driver.find_element_by_xpath("//li[contains(text(),'Cheyenne')]").click()
        self.ui.sleep(2)
        self.ui.driver.find_element_by_xpath("//span[contains(text(),'Submit')]").click()

And another python file, CreateNew.Py

def testCreateNewOtherJob(self):
        self.ui.common.clickNavbarNewJob()
        self.ui.jobs.selectServiceType('Electric')
        self.ui.jobs.selectJobType('Other')
        self.ui.jobs.ProvideElectricJobDetails()

My question is, Instead of hardcoding in the Base.Py as ("//span[contains(text(),'Submit')]"). How can I parse it as text format, so that I could call in the CreateNew.Py as

self.ui.jobs.selectButton("Submit")

Solution

  • If I right understood, you want something like this:

    def selectButton(name):
        self.ui.driver.find_element_by_xpath("//span[contains(text(),'" + name + "')]").click()
    
    
    def testCreateNewOtherJob(self):
        self.ui.jobs.selectButton("Submit") # will click on 'Submit' button
        self.ui.jobs.selectButton("Select AOR...") # will click on 'Select AOR...' button