Running dotnet core Selenium Tests in Azure DevOps

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome in this video I'm gonna show you how to run some dotnet core tests a specifically selenium test in Azure DevOps what you need to accomplish this is an azure DevOps account and Visual Studio 2017 or better in this video I'm going to use Visual Studio 2019 preview 3 so first what I did in Azure DevOps I actually created a project called selenium demos and I have an empty git repo and we're gonna actually use this to store our projects and then do builds and release with that so I'm going to jump into Visual Studio now so I connected to this project and we're basically going to create a new project in this repo so I already done that so we're gonna basically take an MS test dotnet core project we're gonna give it a name so we're gonna call it selenium demo and we're gonna create it so this creates an empty project with basically a unit test in it that we can kind of play with so I'm gonna basically delete this unit test because we don't need it I'm gonna create a new test so we're just gonna say add new item search Google so we now have our tests we're gonna make this public and we're gonna put a little bit of scaffolding into our test project now so under dependency I'm gonna manage my new get projects I'm gonna pick a few things so we're gonna call it we're gonna start with selenium and we're gonna pick under brows we're gonna pick the webdriver so I'm gonna install that we're then gonna install selenium support so we're gonna do some tests with the chrome driver and in order to do that there's a bunch of chrome drivers so if I just look for chrome you'll see that there's a selenium chrome driver then there's a cellular webdriver chrome driver then there's another one from chromium so the one you want to pick is this one here so selenium webdriver chrome driver that's the important one to pick now if we want to run some tests in agile devops we can't pick the latest one right now because the hosted agent don't have chrome 70 and 71 or 72 installs so they only have chrome 68 this will change over time but at the time of this video it there's only 68 install so we need to actually pick 2.40 for this to work so I'm just gonna install this the chrome driver and if we go to update now we'll see that if I remove the X here we'll see that we can upgrade the driver which we don't want to do but we are definitely gonna update the test framework that's the Microsoft framework that comes with the project so first of all we're gonna add the test class attribute to this so that we know it's an actual test class we're gonna make use the unit testing MS test unit testing so we have our class now we're going to create a new test method so we're gonna add the test method tag here public search for cheese let's avoid all right so now we have our method and we'd like to put some code in here so I'm just gonna go and get some code I already have so we now have some code here and we're missing a few using so I'm just gonna basically pick the right using so we want to have chrome here for this we want to have I Oh for this we want to have flexion and for this we want to have selenium and finally for this we want to have support all right so now we have everything going our test is there are basically our our bootstrap is already here so let's run the test now so this will start a browser and it'll go to Google and I'll search for cheese and it'll come back and say that it found cheese Google search as the title of the particular browser one that when it ran some cheese all right so we it's a simple test so it goes to Google it finds the element for where we put our our search parameter sends cheese to it submits it and it waits until it finds the title that starts with cheese and then we basically assert to make sure that it's okay so it's a simple test that's not the point of this video at the point of the video is to show you how to bootstrap everything together so that's fine but it evidently won't run in Azure DevOps because we don't have a browser popping up into the hosted agents so how do we get around this problem so Google offer the opportunity to run some headless tests so what we're gonna do is we're gonna create a new variable called chrome options and here and we're essentially gonna add it at the end of our chrome driver setup so essentially this is one of the important things you want to make sure that you are that the opt has the path where it is in order to be able to basically start because it's looking for the chrome DXE so this is one way of basically setting it up to make sure that the driver knows where it's actually gonna start so pad dot get directory named assembly get assembly location and then we pass the option which says that it's gonna run in headless mode so if I run the same test now we'll see that it's gonna start but we're actually not going to see a browser it's just gonna run behind the scene and come back with our result which is fine so another thing we can do is we can run that from the command line so since we're running dotnet core I can now go into our project and basically I can say dot net test and then selenium slash the project so if I do that this basically will just start the dotnet core engine and it's gonna start a tests and since this is headless it's gonna come back with the run was successful so this is for tests but if we want to run it directly from a binary which we'll need to do in Azure DevOps we would do dotnet vs test and then selenium demo bin divide our test here and we're looking for our DLL and if we do this we can see that we're basically pointing to the DLL directly and that's important because when we build we won't have the c-sharp project into our build output so we actually want to run some tests based on the dll instead so we can run all this from the fanlight and without any Visual Studio and that works fine now the one of the problem with dotnet core is that we need to publish everything that we build including all the packages that are built with this project because if we don't we won't be able to run our our tests in headless mode on a hosted agent so a few other things we need to add is to make sure that this actually works is the Microsoft test platform test host and on the Browse here so we need to add that version 19.9 so this is a dll that's needed to run some tests without having visual studio around so we've installed that which is fine and then the last thing we need to do is we need to add a specific parameter to our projects so that when we publish our projects it's actually going to take the chrome driver and put it in the binary of the published output or else it wouldn't do that so this is the piece of code we need to add so it's called publish chromedriver equals true we're going to save this and now we have a full project that is ready to go to Azure DevOps so the first thing we're going to do is look at our changes so this is all the stuff we added to this project we're going to commit this as the initial version commit this then we're gonna sync our changes and push our change to Azure DevOps and we're gonna actually go to Azure DevOps to continue so here's my repo I'm gonna refresh my repo make sure everything is here so we have our selenium demo and I have a nice button here that says let's create a build for this so we're gonna start we're gonna say that this is an asp net core I'm just taking this template because it has all of the dotnet specifics to actually create my project but essentially I'm gonna change a few parameters so it's not an asp.net application so we're gonna pick this and this creates all the dotnet core steps that we need so the first thing we want to change is we don't want to use the Ubuntu agent pool because we want the chrome driver dot exe to be part of our output and if we use the amoun too it's gonna create a chrome driver without the dot exe and it's actually going to be a UNIX Pacific executable and it won't run after so we're gonna change this and I'm basically going to use the hosted Windows container build agents so we're gonna build all of our stuff here our tests fine so let's go to first of all the restore make sure that this is a restore it's gonna restore all our stuff from our c-sharp project and then we don't have any specifics so we're gonna leave it like that our build command is very simple we want to build the release configuration and then there's nothing else to set up under test I'm actually gonna remove this from here because this is a build so we don't want to run any there's no unit test in this project this is a selenium test so since we don't have any unit tests I don't want to run any tests at this moment here and then finally we want to make sure that we publish our results of that build into VSDs so first thing is remember what I said that it was the spinette specifics so we don't want to publish web projects so that's one thing we don't want to zip our outputs and we don't have the project name into our publish pad so this is all the stuff you want to unpick from the publish task and then we should be good to go so I'm actually gonna basically leave it like selenium demos it so we're gonna basically save and queue and you build and I'll look at our build here alright so this build took about 50 seconds and if we look at the artifacts we can go in a drop open that up we can see that we have our chrome driver we have all of the different dll's that are needed to run we have our DLL for our demo so everything is ready to go now we want to actually execute those tests so how do we do that we want to create a release for this so we're going to click on release here it's actually gonna go and bootstrap release for us and essentially we're gonna create an empty template and we're gonna say that this is our dev stage we're gonna go into our tasks and I'm gonna add one task here which is dotnet core which is quite simple and then we're also gonna add a published task so we're gonna publish our results here so first thing in the Dutton had build I want to if you remember when I was running from the command line I could have run tests now test essentially I need the CS proj in order to run some tests so since I don't have the CH brush I want to run a vs test now it's not part of the basic commands that are available here but I can choose custom and then run the vs test command here now this needs some arguments and the arguments are logger : TR x semicolon log file name equals test dot XML for example right and then the path of the exeed that I actually want to run is star star slash and then we have selenium demo dot DLL right this is the DLL that we have here so we're gonna change this to run [Music] selenium tests and then we're gonna say publish test result we're gonna remove this and then this is vs test results and we called it test dot XML so we're going to leave it like that we're gonna merge our results and we're gonna say a title is selenium tests and then under advanced we want to make sure that we upload the test result file which is there by default and then we can actually save this and we can release this so if we go to release one I'll wait for this to start and essentially this would run the same command I ran from my desktop which was dotnet vs test and then the DLL that is part of our I build output all right so we can see that all of our stuff ran if we actually go into the test tab we'll see that we now have a nice results our test passed took about 35 seconds to run and everything is good so what are the important things to remember now I did a few things in this demo so if we go back to visual studio we'll see that we need to have in our project we need to make sure that we have the selenium webdriver dot chromedriver we need some lambda webdriver selenium support and we need to have the Microsoft test platform test host so those are that at least the minimum prereq that you need then in our project when we start the chrome driver we need to point it to where the Assembly of the DLL is because the chrome driver dot exe is in the same directory so we need to add that in order to run our headless tests we need to have an argument that basically just says headless and finally we need in our project we need to make sure we add the publish chromedriver equals through here once we have all that we should be good to go in our project and we can basically run our tests build and run our tests basically with the.net core tasks in Azure DevOps so I hope this was interesting and thank you for listening
Info
Channel: Etienne Tremblay
Views: 27,599
Rating: undefined out of 5
Keywords: Selenium, Test, Azure DevOps, Dotnet core
Id: e6oHzLhV8PY
Channel Id: undefined
Length: 17min 45sec (1065 seconds)
Published: Thu Feb 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.