Write Python First Selenium script

 

Pre-Requisites:

You have completed Python, selenium, behave installation steps described at the beginning of the tutorial. You have also downloaded the template project, imported in pycharm. Please go through first basic behave scenario 

 

 

Agenda:

  1. Create a new feature file “first_selenium.feature”
  2. Write a Feature and then a scenario
  3. Open www.seleniumframework.com website
  4. Print the title, URL and page source for the website
  5. At the end of this page, a video will be posted

 

1. Right click “features” folder and add a new feature file “first_selenium.feature”

behave_new_feature_file

first_selenium_file

You might see that the file gets populated with the below content already

first_selenium_file_default

2. Replace the content with the below gherkin. As you can see , it is a very simple gherkin

3. Now let’s write the step definitions for it. Click on the line “Given I open …” and on your keyboard press “Alt+enter”. you should see the contextual menu as below. Click “Create new step definition”

alt_enter_step_definition

4. You should see the below menu. Again click “Create new file”

alt_enter_step_definition_create_new_file

alt_enter_step_definition_create_new_file

alt_enter_step_definition_create_new_file_no_git

5. Repeat the step 4 for the line “Then I print the title”, but this time select the file “first_selenium.py” which we already created in Step 4. The screenshots should demonstrate what you might need

alt_enter_second_step_definition

6. Now open the file “first_selenium.py” which is inside the “steps” directory and you should see the below template code that already got generated

first_selenium_template_steps

7. Replace the code with the below code

8. Now go back to “first_selenium.feature” and run the feature by right-click and “Run…feature file”. A browser should pop up and the results in pycharm should look as below.

first_selenium_pass

9. If you execute the same from command line, the output should be as below

first_selenium_pass_cmd

Conclusion

If you have seen the same results described on this page, pat yourself on the back. You have successfully completed your first behave python selenium script and your environment is ready to rock and roll.

 

FAQ

1. My browser doesn’t launch 

Ans: Please check that you have completed the pre-requisites section

2. I get “Empty test suite” error

Ans: The most likely cause of this is a typo in your behave script

3. I need more help

Ans: Please write back to us on the Contact page if you are interested in tutorials

First Selenium Based Script