I am looking for a way to predefine some parameters (URL's) and then run a selenium test for each of them in C#.
Where should I specify those URL's?
How to implement reading of those parameters in my code?
[Test]
public void Test(string URLParameter)
{
driver.Navigate().GoToUrl(URLParameter);
}
And how should I run those tests?
If you are looking for a way to run the same test with a different set of test data i.e: a different URL in your case and If you are using
Either Nunit or xUnit
Please note all built-in attributes allow for data to be provided programmatically, but NUnit does not have any attributes that fetch the data from a file or other external source.
You can make use of
[TestCaseSource]
attribute
Please read more about it from Nunit Documentation
References:
Parameterized-Tests
Custom-Attributes
There are several ways to predefine and keep such test data.
You can keep it in some external file. It can be JSON, XML or plain text formatted file.
It may be kept in some resource C# class file inside the project.
It can be kept in some kind of DB etc.
Actual implementation of how to read this data will vary according to the way you decided to keep the test data, according to your project structure etc.
There are several best practices how to do that, not only one way to do that.
There are also several ways to run such tests. You can learn about these best practices on many online tutorials and other resources.
For that purpose you should use multiple different ways to keep test data.
1- Kept in the resources folder of the project for example in
properties file.
2- Kept data in DB and read from there while
needed.
3- Kept in some external sources and read/retrieve if from
there.
Note: Should use that method which will be convenient for you later on.
Related
I have a solution with several self-contained classes and methods. For example, I have:
a FileDownloader class that has multiple different methods that download files based on passing in a URL or multiple URLs
a DataTransformations class that has multiple methods that transform data depending on what is necessary for a given operation
a FileWriter class that writes some data to some kind of file type or file format
etc.
I have all of these classes as .cs files under the same solution. I can consider the order of executions for some specific operation and call the methods from MAIN in the correct order and it produces the output that I expect. I will eventually, however, need to call some or all of these methods in many different configurations for several different processes and I don't know how to do that. I know how to pass in configuration through command line arguments, but even that requires the specific order and number of methods called stays the same between processes. This is not tenable because I will not need to download files in some instances and I will not need to transform data in some instances etc. I am very new to .NET development and I have not yet wrapped my head around how to truly decouple these classes from each other. Do I have to deploy a different solution for each class? I would like to just be able to say "call file downloader with these parameters" and then "perform data transformations based on these parameters" basically like steps in an execution job.
Dirty Answer, Compile as a library and then add a reference in whatever project you want to use those methods for. you can then call the methods by name (LibraryName).MethodName(Parameters). of course you will need to always have that DLL in whatever other project need access. if you have any questions on how to do this let me know.
I am debugging a .net(C#) app in VS2013. I am calling a rest service and it returns data as a list of phone calls that have been made between two given dates.
So the structure of List is something like CallDetails.Calls and then Calls property has several child properties like, call duration, timeofcall, priceperminute etc etc.
Since I am debugging the app, I want to avoid every time hitting the server where the rest service is hosted.
So my question is simply that if there is a way that when I have received the List of data items, I (kind of) copy and paste data into a file and later use it in a statically defined List instead of fetching it from the server?
In case someone wonders why would I want to do that, there is some caching of all incoming requests on the server and after a while it gets full so the server does not return data and ultimately a time out error occurs.
I know that should be solved on the server some how but that is not possible today that is why I am putting this question.
Thanks
You could create a unit test using MSTest or NUnit. I know unit tests are scary if you haven't used them before, but for simple automated tests they are awesome. You don't have to worry about lots of the stuff people talk about for "good unit testing" in order to get started testing this one item. Once you get the list from in the debugger while testing, you could then
save it out to a text file,
manually (one time) build the code to copy it from the text file,
Use that code as the set-up for your test.
MSTest tutorial: https://msdn.microsoft.com/en-us/library/ms182524%28v=vs.90%29.aspx
NUnit is generally considered superior to MSTest, and in particular has a feature that is better for running a set of data through the same test. Based on what you've said, I don't think you need the NUnit test case, but I've never used it. If you do want it, after the quickstart guide, the keyword to look for is testcase if you want to go that route. http://nunitasp.sourceforge.net/quickstart.html
I have a series of MS Unit tests in a class that I have created called Forename. The all run and pass sucessfully to test a variety of inputs e.g 100 chars max etc
I am now looking at getting CodedUI to find the forename and execute these test. I have managed to do this for one specific web page that has the forename control. I have managed to get this all working and pass.
I have now introduced a second page and I want to re-use the same set of test methods, and not have to repeat the code. I can defind and interface and implement and extract some methods to allow some re-use. I have tried to use inheritace, but am struggling and need some guidance if this is possible with MS Test.
Ideally I want to navigate to the page and run the forename tests. I then want to go to the second page and execute the extact same test.
All help appreciated.
It looks like you might want some guidance on how to set up CodedUI tests in such a way that they become maintainable. I can recommend you look at a concept called the PageObject pattern.
Page Object pattern
You can do this with either the UIMap files you record, but personaly I like a code First approach better. You can find more details on writing codedUI without UI Map files here
It not only describes how you can map your web application to page objects, it also describes a way to build build out a Fluent API that makes your tests very easy to read and better maintainable.
Hope that helps.
I have a method ExportXMLFiles(string path) to export xml files at a certain path with some elements inside it like FirstName, LastName, MajorSubject. These values are getting picked from a database.
Now I need to write a Unit test method for it and I have not worked on much unit tests except simple and straight forward ones. My confusion is, do I need to connect to database and create a XML file or do I need to pass hard coded values while creating XML file so that I can validate the values in XML created?
Is there any other way for doing this?
You absolutely do not want to use an actual database in your unit test. It adds one level of complexity that you don't want to deal with in your unit tests. It also makes your unit tests less reliable and slower. See if you can break the database functionality into an interface that you can instantiate using a mocking framework. Try looking into something like moq or if that isn't enough check out moles from Microsoft .
Edit - Another post mentioned that if the functionality is to write to the disk then your unit test should validate that the file was written out to disk. Using Moles you can simulate file systems and test your file system calls and simulate write failures or whatever other cases you need which would give you a lot more flexibility and speed than actually physically writing to disk. Things like a disk write failing would be miserable to test without something like moles.
A unit test should be small in scope and isolated from dependencies eg databases and file systems. So what you want to do is look at mocking out the database access and what would get written to a file so that you can run your test without needing particular values in the database. Unit tests should be fast to run, have repeatable results (ie run twice, get the same answer), isolated from other tests and able to be run in any order.
A unit test is looking at ONE item of functionality and not relying on the behaviour of anything else.
So look at using a pattern such as dependency injection so you can provide (ie inject) database and file system dependencies. Look at a mocking framework such as NMock or write your own lightweight fake objects that implement the same interface as the dependencies and then you can pass those into your functions being tested.
What is the responsibility of this method ?
Is it to dump given data in the form of xml files at a certain path? If yes, then you'd have to check that the files are in fact created.
This is not a unit test but an integration test (because this is the class at the boundary between your app and the filesystem). You should abstract away the input data source (the DB) via an interface/role. You can also create a Role to CreateXmlFile(contents) but I think that's overkill.
// setup mock data source to supply canned data
// call myObject.ExportToXml(mockDataSource, tempPath)
// verify files are created in tempPath
Finally this class needs to implement a Role (DataExporter) so that the tests that use DataExporter are fast / don't have to deal with filesystems (or XML).
I am writing a Selenium test to validate an input form on my web page. I would like to cover all combinations of input data, but I would not like to write a separate test for each. Right now, I'm using an excel spreadsheet as a data source and I have the combinations listed as each row.
I was hoping there would be a way to cover all the cases without needing to use the excel file or write a separate test for each case. Is there anything that can help with this?
If you can come up with each possibility you want to test for each parameter, then you can do a quick cross-join using multiple from statements in LINQ syntax to come up with every combination of those possibilities.
You may also want to look at Pex. It can analyze your class and generate test methods to test every possible code path. It can be really useful for finding those corner cases that you might not have thought of on your own. Of course, this is only useful if you've written the class in a unit-testable manner. It may not help if your web page's form isn't backed by an MVC action or something of that sort.
do you just need a way to get all combinations to values?
You can do this with linq and various other techniques - see this questions as an example
So generate all input-combinations for your method and then just write a Unittest (potential very long running) in mstest or whatever to check each one.