How to run Data-Driven Tests with Selenium Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
in today's video we're going to talk about how you can perform data driven testing for your own software in Python so first let me discuss what data driven testing is so data driven testing is a software testing approach where test cases are designed based on different sets of data rather than fixed inputs it involves executing the same test script with multiple dat ass sets to validate the behavior of the application under various conditions the purpose of data jouren testing is to increase test coverage identify potential defects and verify that the application functions correctly with different input values so the benefits of data testing so first obviously it's improved test coverage with a wide range of input data more scenarios are covered and this just increases the chances of finding defects the second is reusability by separating test data from test scripts data sets can be reused across different tests saving time and effort and finally scalability it's easier to scale data driven tests to handle larger data sets without modifying the test scripts so this basically will show this so um there's some other um uh some other advantages and benefits of D testing uh so right here uh maintainability is also another advantage Vantage so um changes in test data can be made independently of test scripts making maintenance more straightforward uh and then also flexibility testers can quickly add new test data to verify additional scenarios so overall data-driven testing is particularly useful when application has a large number of test cases with simar functionalities and when there is a need to validate various combinations of put data by adopting this approach software testing becomes more efficient effective and adap adaptable to changing requirements so later in this video I'll show you how we can uh do a simple example of using data driven testing in Python and um yeah so right here I've opened my python um code that I had from the last two videos if you haven't watched those videos already I'd suggest you go back and look at those videos because this does continue off of those videos um and basically these are the code that we have from the previous videos but um if you don't want to watch those videos that's totally okay as well because I won't be uh explaining off of the knowledge from those videos so for data driven testing so I guess the first thing I'm going to do is I'm going to go ahead to this main folder here and I'm going to create a new directory and I'm going to call it data under this directory let me make a file so I'm going to create a file and I'm going to name it uh test.csv and under this test.csv file file I'm just going to copy and paste some stuff over um so copy this and paste that and then go ahead and save this and now I can close this so you'll see later why I did this but um basically um you basically this is some of the data that we'll be using later now next what I'm going to do is I'm go under a test folder and I'm going to create a new uh file for today so I'm just going to be a python file and I'm going to name it testore selenium data dpy and notice that I used testore um this is a requirement uh for naming of your files if you're using U pest but um if you want to know exactly why I would suggest you go back and watch the previous videos again but just uh just I just want you to note that if uh you don't intend to go back and watch the previous videos but yeah so first thing I'm going to do is I'm going to import some packages on top so I'm just going to go ahead and paste some stuff so the first thing we import is we import time we import um from selenium we import web driver we import some other stuff like the buy the pi test import CSV as well now after I imported these things um give me one second just going to see what is going on here why is it gray out but um yeah so next thing I'm going to do is I'm actually going to show you how we can um basically uh do uh do data driven testing so first I'm going to define a new function and this is going to I'm actually going to start with this tag it's just py test dot fixture and I'm going to so this is a tag basically saying that we can pass this function in later into another function as a parameter so I'm going to Define it and call it Firefox and essentially what this function will do is it will start a web driver for Firefox so I'm just since this is something that we've seen a lot of times already I'm just going to go ahead and just copy and paste it so we start a web driver Firefox we maximize the window and so on now we can go on to the portion that's specific to data driven testing so what I'm going to do is I'm going to create a tag py test. mark. parameterize py test mark. pize and inside here I'm going to name something Golf and now I'm going to Define golf as this list right here so copy a list so I Define this golf as this list right here and then next I'm going to define a function called testore search golf and I'm going to pass the Firefox function from above because it has a fixture tag and then I'm going to pass golf from above as well and now inside here what I can do is I can do Firefox doget uh and I'm going to use the URL to my website that we're testing on so I'm going to go ahead and paste that URL so this is the website we'll be testing on and then I'm going to say we're going to sleep for three seconds once we get to that website and then what we're going to do is we're going to search uh we're going to define the element search box using um the by name as and we're going to say it search string uh and again if you're not familiar with this what you can basically do is for example if I go to the website that we want to test um which is under here golf right here if I go to search here I click right click and I click inspect uh and then right here you see that by name is search screen so that's how I did it basically but yeah so Define search box now I Define the search button and again we'll Define this as uh using this as well so go ahead I'm just going to paste over because I don't want to mess up the elements uh so Firefox doind element this time we're using X path instead which is another locator that you can find using the same method uh and then after that basically what we're going to do now is we're going to say for the search box we're going to send keys and what keys are we going to send well we're going to send our parameters uh golf and then we save that for the search button after you send those keys we're going to click the search button so that it actually searches for it and now go ahead and save and let me run this for you and see what happens so I dragged it over and then basically you see that it's searching for the golf course uh and it's found Tiger golf um and actually every time it opens a new window so I just have to drag it over because it opens on my other window and again it searches for golf course and it'll open another window on my other uh monitor as well I drag it over maximize it we'll see that it searches this golf course and click search and so those are the three tests that we tested for uh which is Tiger golf golf locations and Heritage Point Golf Course so that's basically how you can uh do data driven testing with these different uh variables that you define using the parameterize uh T tag uh next I'm going to define a function and I'm just going to Define it as such I'm going to call it load CSV and basically what it does is it reads through this CSV file and it will um it will read that CSV file and basically it will read it and then it will load it into a list um and for this I'm actually I think I messed up The Path right here uh this is not a python selenium project Instead This should be Python tutorial one so tutorial one and then we have that so now after that let me Define the next thing so this time instead of using so this time we'll still use the same parameter actually it'll be p test. mark. parameterize paramet and we're going to still Define it as golf but this time we can Define golf using this load CSV right here and so we're defining it as whatever this thing this function returns and under that I'm going to make a new function it's going to be test select country this time we're testing for a different functionality we're passing Firefox and golf as well but then uh under there we'll do the same stuff in the beginning so um essentially right here we see that we're doing the same stuff still um we're basically searching for um we're basically going to this URL we're going to find these two different elements and then after that what we'll do is we'll actually do select C which is this first element is where you enter your filter we're going to send Keys again and we're going to send gol and then we're going to do filter. click filter. click click and let me run this and show you what's going on so again it'll open on my other monitor so I have to drag it over but uh here we go I dragged it over under this country right here it'll select Australia and it filters uh now it goes on to the next test remember we had four entries into our CSV file so I'll drag this over again again it'll select the country here Canon and we'll drag it over uh two more to go maximize it'll select a country up there Iceland and then final one I pull this over maximize it it'll select the country again right here Mexico and filters it and now it's all done so I'll go ahead and close these windows um and it says for test passed so yeah so this is how you can perform data driven testing in Python I just showed you a simple example using different a CSV file uh and just when you determine and Define your own uh variables but yeah if you found this video helpful please give this video a like And subscribe to our Channel and we look forward to seeing you next time thank you for listening
Info
Channel: Test Automation 101
Views: 114
Rating: undefined out of 5
Keywords: Selenium, Python, automated testing, data-driven testing, Selenium WebDriver
Id: qt7DB4IuX5k
Channel Id: undefined
Length: 12min 41sec (761 seconds)
Published: Mon May 13 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.