i am trying to use restsharp with my api testing. I need help with my code below, to make it right. I'm trying to make response with adding parameters on a different tests. I tried different ways, but I am a total beginner in programming.
OrdersGate:
public class Authenticate
{
public string userLogin { get; set; }
public string authenticateKey { get; set; }
}
public class Client
{
public string clientLogin { get; set; }
public string clientFirstName { get; set; }
public string clientLastName { get; set; }
public string clientCity { get; set; }
public string clientEmail { get; set; }
public string clientHasTaxNumber { get; set; }
public string clientSearchingMode { get; set; }
public string clientFirm { get; set; }
public string clientCountryId { get; set; }
public string clientCountryName { get; set; }
}
public class OrdersDateRange
{
public string ordersDateType { get; set; }
public List<string> ordersDatesTypes { get; set; }
public string ordersDateBegin { get; set; }
public string ordersDateEnd { get; set; }
}
public class OrdersSerialNumberRange
{
public int ordersSerialNumberBegin { get; set; }
public int ordersSerialNumberEnd { get; set; }
}
public class OrdersRange
{
public OrdersDateRange ordersDateRange { get; set; }
public OrdersSerialNumberRange ordersSerialNumberRange { get; set; }
}
public class AuctionsAccount
{
public int auctionsAccountId { get; set; }
public string auctionsAccountLogin { get; set; }
}
public class AuctionsClient
{
public string auctionClientId { get; set; }
public string auctionClientLogin { get; set; }
}
public class AuctionsParams
{
public List<string> auctionsServicesNames { get; set; }
public List<int> auctionsItemsIds { get; set; }
public List<AuctionsAccount> auctionsAccounts { get; set; }
public List<AuctionsClient> auctionsClients { get; set; }
}
public class OrderSource
{
public int shopsMask { get; set; }
public List<int> shopsIds { get; set; }
public AuctionsParams auctionsParams { get; set; }
}
public class Product
{
public int productId { get; set; }
public string productName { get; set; }
public string sizeId { get; set; }
public string sizePanelName { get; set; }
}
public class Packages
{
public List<string> packagesNumbers { get; set; }
public string orderHasPackageNumbers { get; set; }
}
public class Stock
{
public int stockId { get; set; }
}
public class Campaign
{
public int campaignId { get; set; }
public List<string> discountCodes { get; set; }
}
public class OrdersBy
{
public string elementName { get; set; }
public string sortDirection { get; set; }
}
public class OrdersGet
{
public string orderPrepaidStatus { get; set; }
public List<string> ordersStatuses { get; set; }
public List<string> couriersName { get; set; }
public string orderPaymentType { get; set; }
public List<string> withMissingSalesDocuments { get; set; }
public string orderType { get; set; }
public string dropshippingOrderStatus { get; set; }
public List<string> ordersIds { get; set; }
public List<int> ordersSerialNumbers { get; set; }
public List<Client> clients { get; set; }
public OrdersRange ordersRange { get; set; }
public OrderSource orderSource { get; set; }
public List<Product> products { get; set; }
public int resultsPage { get; set; }
public int resultsLimit { get; set; }
public string clientRequestInvoice { get; set; }
public Packages packages { get; set; }
public List<Stock> stocks { get; set; }
public Campaign campaign { get; set; }
public string loyaltyPointsMode { get; set; }
public string orderOperatorLogin { get; set; }
public string orderPackingPersonLogin { get; set; }
public List<OrdersBy> ordersBy { get; set; }
public string searchingOperatorTypeMatch { get; set; }
public string ordersDelayed { get; set; }
}
public class RootOrdersGet
{
public Authenticate authenticate { get; set; }
public OrdersGet @params { get; set; }
}
My code:
[TestFixture]
public class Get
{
private static object[] DataOrdersGet =
{
new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "unpaid"},
new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "restored"},
new object[] { GlobalVariables.login, Modules.GenerateKey(Modules.HashPassword(GlobalVariables.pass)), "waiting"},
};
[OneTimeSetUp]
public void BeforeAnyTests() { }
[Test, TestCaseSource("DataOrdersGet")]
public void SprawdzZamowieniaODanymStatusie(string login, string pass, string orderPrepaidStatus)
{
string vGate = "100";
RestClient restClient = new RestClient(GlobalVariables.url + "api/?gate=orders/get/" + vGate + "/json");
RestRequest restRequest = new RestRequest(Method.POST);
var Authenticate = new Dictionary<string, string>
{
{ "userLogin", login },
{ "authenticateKey", pass }
};
var Param = new Dictionary<string, string>
{
{"orderPrepaidStatus", orderPrepaidStatus}
};
var body = new
{
authenticate = Authenticate,
@params = Param
};
restRequest.AddJsonBody(body);
restRequest.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse restResponse = restClient.Execute(restRequest);
Console.WriteLine("Status: " + restResponse.StatusCode);
Console.WriteLine("\nResponse: " + restResponse.Content);
Console.WriteLine("\nRequest: " + body.ToString());
}
[OneTimeTearDown]
protected void CloseAfterAllTests() { }
Right now, i am adding parameters by dictionary. How to make it better without it? I know that there is something like restRequest.AddParameter()
for that. But I don't know how to use it, with my gate.
You can try doing it this way:
var body = new {
authenticate = new Authenticate() {
userLogin = login,
authenticateKey = pass}
},
@params = new OrdersGet() {
orderPrepaidStatus = orderPrepaidStatus
}
}
// Adds the entire object to the request
// after serializing it and making it json
restRequest.AddJsonBody(body);
// Executes the request with the proper payload
IRestResponse restResponse = restClient.Execute(restRequest);
Console.WriteLine("Status: " + restResponse.StatusCode);
Console.WriteLine("\nResponse: " + restResponse.Content);
Console.WriteLine("\nRequest: " + body.ToString());