0

I'm using Python Behave to run automated tests against a web application.

There is a login page that takes a username and password.

The Behave scenarios have different login requirements, for example;

Given I am logged on as user1
Then do something

I created a csv file to hold the username and passwords for each login, but I'm having trouble getting it to read only the details that I want.

The csv file is like this;

 usertype,username,password
 user1,somename1,somepassword1
 user2,somename2,somepassword2

The required user is passed in the Behave step definition;

 @given('I am logged on as {login}')

I have tried the following implementation;

def do_login(context, login):
    driver = context.browser

    user_data = csv.DictReader(open('users.csv'))
    for row in user_data:
        row['user'] = login
        uname = row['username']
        pword = row['password']

        driver.find_element_by_name('username').send_keys(uname)
        driver.find_element_by_name('password').send_keys(pword)

But this just logs in with the first user in the table.

I don't know how to get it to select only the username and password corresponding to the login.

So for example, I pass the login as user2 and I want the username as somename2 and the password as password2.

I've read other questions relating to reading from csv files, but I couldn't find one that was the same as this.

I'm quite new to Python.

TonyM
  • 11
  • 3

2 Answers2

2

You should proof with an if statement in the for loop if the usertype matches the login before doing the send_keys method.

def do_login(context, login):
    driver = context.browser

    user_data = csv.DictReader(open('users.csv'))
    for row in user_data:
        row['user'] = login
        uname = row['username']
        pword = row['password']

        # something like this
        if login == row['usertype']:
            driver.find_element_by_name('username').send_keys(uname)
            driver.find_element_by_name('password').send_keys(pword)
colidyre
  • 4,170
  • 12
  • 37
  • 53
  • This doesn't quite work. It just tries to enter each username and password in sequence. – TonyM Sep 21 '15 at 08:31
  • Yes, because of the for loop you're using. You have to iterate over a list and check if it's the right row at the moment. You can `break` then or you can use other data type like `dict` or something. But you have to iterate over CSV file anyway. There's no significant performance advantage I can see. – colidyre Sep 21 '15 at 09:02
  • I've got it working now, with a slight change in your answer. Thanks for the help. – TonyM Sep 21 '15 at 11:30
0

I am not very much cleared with your question, but you can try this

Just read csv data in list of Dict format

user_data = csv.DictReader(open('users.csv'))
user_list = [d for d in user_data]

now you can use this data how you want as :

user_list[row_index][column_name]

I will suggest better to store you login information/Logs in any database system.

Roman Marusyk
  • 23,328
  • 24
  • 73
  • 116
OmPrakash
  • 198
  • 1
  • 9
  • I've updated my original question with a snippet of the behave scenario. I want to login as a user specified in the scenario. In your answer, would the row_index and column_name relate to the usertype, username in the csv? So should I be able to do ['usertype']['username'] – TonyM Sep 21 '15 at 08:34
  • If you really want to use the [DictReader](https://docs.python.org/2/library/csv.html#csv.DictReader) (that's another data type also mentioned in [comment](http://stackoverflow.com/questions/32634396/python-read-csv-cell-from-specific-row-row-index-is-passed-in-variable/32634714#comment53225830_32634714)), you have to read it with the `fieldnames parameter`. It seems that you have a header with fieldnames in csv file, so you can omit *fieldnames parameter* and access the resulting dict like this: d['username'] or d['password']. But you have to iterate anyway (for loop or list comprehension). – colidyre Sep 22 '15 at 23:33
  • can do like that:- user_data = csv.DictReader(open('users.csv')); user_list = {d.pop('user_type'): d for d in user_data}; now you can access value using user_list[user_type][column_name]; – OmPrakash Sep 23 '15 at 05:17