0

I am working with selenium webdriver in python. I have a script:

if flag=='e':
    try:
        flag='e1'
        login (user, passwd)
        flag='e2'
        my_script(data)
        flag='e3'

I have this set up so that I can find out at what step any failure occurs at. This works fine for a small script, but if it was larger this would be difficult. I wondered if there is a way to execute a python command or block of code after each step in the script. so if the selenium scipt looked like:

step A
step B
step C
......
step Z

I could set flag = 0 and increment the flag after each step so that the effect is:

step A
flag = flag+1
step B
flag = flag+1
step C
flag = flag+1
......
step Z
flag = flag+1

Can this be done ? This is more of a general python question than selenium related although I would use it mainly in selenium.

1 Answer 1

1

Selenium itself does not provide a facility for this. What you should do is used a proper testing framework to run your tests. They typically have some facility that allows you to run repetitive code before or after each test.

For instance Behave has an after_step callback that is executed after each step. (It so happens that Behave actually has a notion of "step" in its testing model: a feature contains scenarios which themselves contain steps.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, that's interesting, and good info. For now I want to stick with my selenium scripts the way they are, since I have a large number and don't want to try to convert them to behave scripts. However your idea of using a callback is interesting. Does this problem fall into the category of functional programming or meta-programming in your estimation? I'm pretty new to programming and I've heard the buzzwords but as you can see from my question, I'm not sure how to approach this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.