Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

i have a drop down list that i add items to it,i ask the user to type in brand name and check first if that brand is already found on the drop down list if yes prints a message if no do some actions. here is the code:

def add_brand(self,x):
     self.elem=self.driver.find_element_by_id('MainContent_txtBrand')
     self.elem.send_keys(x)
     self.txt=self.driver.find_element_by_id('MainContent_ddlBrand')
     self.brands=self.txt.find_elements_by_tag_name('option')           
     for i in self.brands:
            if i.text==x:                
                print('already found')
                break
            else:                
                print('this item is new')                
                self.driver.find_element_by_id('MainContent_hlSaveBrand').click()
                self.driver.find_element_by_id('MainContent_txtBrand').clear()
                break

and here is the code to call that function:

no=input('enter how many brands u want to add')
store.add_brand_model_item()
for i in range (0,no):
    koko=raw_input('plz enter brand name')
    store.add_brand(koko)
share|improve this question
    
I think I understand the purpose of the code, but what is the question? –  user246 Nov 28 '12 at 14:17
    
i need to check if the brand the user will enter is not listed in the DDL if listed a message "already found" will show up –  engy Nov 28 '12 at 14:57
    
when user enter any brand even it's a duplicated one it compares with only the first <option> in the DDL then it turns to run the else statement ,i want to loop over all DDL options first –  engy Nov 28 '12 at 14:59
    
Did you try inserting something like print("x >"+x+"< i.text >"+i.text+"<") before the if? –  user246 Nov 28 '12 at 15:19
2  
Somewhat confused about what this has to do with testing ? –  Phil Kirkham Nov 28 '12 at 16:21
show 7 more comments

1 Answer

up vote 0 down vote accepted

I googled a lot but finally I found the solution. I know it's not the only solution, so if anyone has another point of view to solve this, please share as I mainly want to learn the Python language and testing.

 def add_brand(self,x):
    self.elem=self.driver.find_element_by_id('MainContent_txtBrand')
    self.elem.send_keys(x)
    self.txt=self.driver.find_element_by_id('MainContent_ddlBrand')
    brands=self.txt.find_elements_by_tag_name('option')
    items=[]
    for i in brands:            
        items.append(i.text)

    item=items.count(x)
    #print(item)
    #print(items)
    if item>=1:                    
        print('already found')
        self.driver.find_element_by_id('MainContent_txtBrand').clear()
    else:                
        print('this item is new')                
        self.driver.find_element_by_id('MainContent_hlSaveBrand').click()
        self.driver.find_element_by_id('MainContent_txtBrand').clear()

The list is like a magic salute to data structures.

share|improve this answer
    
i know that this function is like validating the entry to the application and this has nothing to do with testing as from testing prospective you shouldn't validate the entry yourself you should let the application show us what it has to detect the bugs.i was only trying some stuff with the webdriver.thanx for help every body –  engy Nov 29 '12 at 11:23
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.