I did do my best to erase the sensitive information but if someone see's any please help me erase it thanks.I have written a program in which I am getting the information about an entity from/with API and than need to verify it against the website. My code is as follow.
namespace authtest
{
class Program
{
static async Task Main(string[] args)
{
Console.WriteLine("MY API");
AccessToken token = GetToken().Result;
Console.WriteLine(String.Format("Access Token: {0}", token.access_token));
Console.WriteLine("Got the token");
Console.Write("Enter P");
string Id = Console.ReadLine();
Rootobject RespObjFromAPI = await CallAPI(token,Id);
Console.ReadLine();
}
public static async Task<Rootobject> CallAPI(AccessToken J, string id)
{
var client = new RestSharp.RestClient("URL");
var request = new RestSharp.RestRequest("code", RestSharp.Method.GET);
request.AddParameter("P", id);
request.AddParameter("Authorization", "Bearer " + J.access_token, ParameterType.HttpHeader);
RestSharp.IRestResponse resp = client.Execute(request);
string JsonText = resp.Content.ToString();
Rootobject myobj = JsonConvert.DeserializeObject<Rootobject>(JsonText);
CodeResults P = myobj.lookup[0].codeResults;
foreach(Code x in P.Code2)
{
Console.WriteLine(x.VALUE TO BE VERIFIED);
}
return myobj;
}
public static async Task<AccessToken> GetToken()
{
Console.WriteLine("getting TOken");
string clientId = "MY CLIENT ID";
string clientSecret = "MY CLIENT SECRET";
string credentials = String.Format("{0}:{1}", clientId, clientSecret);
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes(credentials)));
List<KeyValuePair<string, string>> requestData = new List<KeyValuePair<string, string>>();
requestData.Add(new KeyValuePair<string, string>("grant_type", "client_credentials"));
FormUrlEncodedContent requestBody = new FormUrlEncodedContent(requestData);
var request = await client.PostAsync("Auth URL", requestBody);
var response = await request.Content.ReadAsStringAsync();
return JsonConvert.DeserializeObject<AccessToken>(response);
}
}
}
}
Above Written API code works perfectly as long i am running it separately, Following is my Selenium Code,
namespace Selenium
{
[TestClass]
public class UnitTest1
{
static IWebDriver driver;
[TestMethod]
public void A_VerifyHeader()
{
var verifycode = driver.FindElement(By.XPath("tag value to be verified against api data")).Text;
Task<TestResult> task = Program.CallAPI();
task.Wait();
Assert.AreEqual(task, verifycode);
}
[AssemblyCleanup]
public static void Cleanup()
{
driver.Quit();
}
[AssemblyInitialize]
public static void Setup(TestContext context)
{
driver = new ChromeDriver();
driver.Navigate().GoToUrl("website URL");
driver.Manage().Window.Maximize();
driver.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(30);
}
}
}
IF i run it without the Calling the task in the selenium test i can find the tag and all, BUt when I try to call Task task = Program.CallAPI() i get errors I also tried calling Task task = Program.CallAPI(AccessToken J, string id). I have tried different things trying to achieve this but no success.