Wednesday, December 4, 2019

Easy ways to identify elemants

IWebElement el4m = Driver.FindElement(By.CssSelector("input[type='button']value='Save User Roles']"));


IWebElement el4m = By.CssSelector
("button[onclick='new UserManagement().LoadExcelExportSearchResult();']")

Valid format for css selector for above format is :

The valid format is tag[attribute="value"]



Sunday, August 25, 2019

How to open exisiting chrome web browser ( with original History, Cache and Cookies ) using selenium C#

As we all know, when we say  driver = new ChromeDriver()
it will open a new chrome window with no Cache ,History or Cookies.

But in 1 day I had a requirement to open the existing google chrome web browser and continue my selenium tests on that browser.
So this is how I did it.


Go to chrome://version/ and locate the Profile Path.
Copy entire Default folder and copy it to some where else.

e.g- C:/Driver/test

then set your code like below.

ChromeOptions options = new ChromeOptions();
options.AddArguments(@"user-data-dir=C:\Driver\test");

driver = new ChromeDriver(options);

Monday, July 8, 2019

How to get a custome HTML report from Azure build pipeline for a unit test project

...This might not be the best option but it works for me...


Normaly you will have your test cases on a one test project.
In our case I add another project to the same soulution as on the below image.


That 2nd project will contain nessosory steps to:
  1. Locate the TRX file which is genarated by the Azure build pipeline (vstestconsol.exe).
  2. Convert that file to HTML.
  3. Email it to a given email address.
So the plan will be, I will copy the original TRX file to a different location / folder.
Rename the TRX file.
Then using "TrxerConsole.exe" genarate the HTML file and email it.

So as the 1st step add the 2nd project it will also a unit test type project.

Add below methods to your newly created unit test project which rename the original TRX.



        [TestMethod]
        public void RenameOrgTRX()
        {
            string sourcePath = @"d:\a\1\s\UnitTestProject1\CleanUp\ReportCreator\";
          
           string[] sourcefiles = Directory.GetFiles(sourcePath,"*.trx");

            foreach (string sourcefile in sourcefiles)
            {
                string fileName = Path.GetFileName(sourcefile);
                File.Move(sourcePath+fileName, sourcePath+"report.trx");
            }
        }

Then 2nd method to genarate the HTML file.
     [TestMethod]
        public void GenerateHTML()
        {
            Process proc = null;
            proc = new Process();
            proc.StartInfo.WorkingDirectory = @"d:\a\1\s\UnitTestProject1\CleanUp\ReportCreator";
           
            proc.StartInfo.FileName = "TrxtoHTML.bat";
            proc.StartInfo.CreateNoWindow = false;
            proc.Start();
            proc.WaitForExit();
        }

At this moment create a new folder at your 2nd project named "ReportCreator".
Then create a bat file inside the ReportCreator folder.

bat file will looks like this.
"TrxerConsole.exe report.trx" - So in side the bat file im calling TRXERconsole.exe which can convert trx to HTML.
Report.trx will be the TRX name.

At the 1st method we have successfully rename the autogenarated TRX file to report.trx so im sure that the TRX name will alwasy be the same.
After runinng this method you will get a HTML file named report.trx.html which is the final test report.


As the 3rd method add a send email method to send an email with attaching the newly created report.trx.html file.

 [TestMethod]
        public void SendEmail()
        {
            string fromAddress = "XXXXX@gmail.com";
            string mailPassword = "XXXX";
            SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
            SmtpServer.Port = 587;
            SmtpServer.EnableSsl = true;
            SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SmtpServer.UseDefaultCredentials = false;
            SmtpServer.Credentials = new System.Net.NetworkCredential(fromAddress, mailPassword);

            MailMessage myMail = new System.Net.Mail.MailMessage();
            myMail.From = new MailAddress("XXXXX@gmail.com");
            myMail.To.Add("XXXX@gmail.com");
            myMail.To.Add("XXXX@tiqri.com");
        

            myMail.Subject = "TeckTalk";
            myMail.Body = "Please download the attached HTML file to view the API status";
            string attachmentFile = "d:\\a\\1\\s\\ReportGen\\NewFolder1\\report.trx.html";
            Attachment attachment = new Attachment(attachmentFile, MediaTypeNames.Application.Octet);
            myMail.IsBodyHtml = true;
            myMail.Attachments.Add(attachment);
            SmtpServer.Send(myMail);


        }



Lets create the pipeline.


This is how the pipeline looks like.
Red color task will be the original test task whic contain all the the unit tests.
Green task will be the copy file task.
Yellow task will contain 2nd test method which contain  Rename, GenerageHTM and SendEmail methods.

YOU ARE DONE.



Thursday, June 27, 2019

How to execute Bat file or command line arguments inside a unit test

             Process proc = null;
            proc = new Process();
            proc.StartInfo.WorkingDirectory = @"C:\Driver\NEW2\";
            proc.StartInfo.FileName = "TrxtoHTML.bat";
            proc.StartInfo.CreateNoWindow = false;
            proc.Start();
            proc.WaitForExit();

WorkingDirectory will change the starting location of the CMD.

Vstest.console.exe not discovering any of the tests


"No test is available in <path> Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again."


My initial plan was to show code coverage on sonar dashboard for a unit test project.

First, I get the .coverage file using below command

%CodeCoverage% collect /output:"C:/XXXXX/VisualStudio.coverage" "C:/Program Files (x86)/Microsoft Visual Studio/XXXXX/vstest.console.exe" "C:/Program Files (x86)/XXXX/XXX.dll"

Then convert it to
coveragexml.

So when I got the project I build it ,done a Nuget reset.
After that point, the test dll file to the vstest.console.exe and try to genarate the .coverage.
However, it says,
"No test is available in my test dll. Make sure that test discoverer & executors are registered and platform & framework version settings are appropriate and try again."
But when i open the solution and execute the project using test explorer at the VS it detect the tests and everything is working fine.

Initial project was developed by visual studio 2017 and at the QA environment we have visual studio 2015.(vstest.console.exe is also came with the VS 2015).

I tryed for several days to resolve this but no luck.
Then I start installing 2017 VS to the QA environment.

Then again do the same thing, Run the command by pointing the test dll and vstest.console.exe, which comes with visual studio 2017.

Bingo it works and it identify the unit tests inside the DLL file.

So the issue was with the wrong vstest.console.exe,




Wednesday, June 5, 2019

Test reporting for MSunit testing

  1. After you have finished your code about test, go build=>rebuild the solution. (no need to use test explorer)
  2. When rebuild succeeds, find the .dll file of your unit test project and copy the path of this file. (In my test, it’s UnitTestProject3.dll)
  3. Open the ‘developer command prompt for VS’. And then type “cd path” command to locate the file which contains the dll file. (The path is what you copy in step2)
  4. After that, type “vstest.console.exe UnitTestProject3.dll /logger:trx”. Then you can see the test result in the window, also you can find the sentence like this “Results file: …path\name.trx”
  5. That is the test result file you want, and you can convert it from xml to HTML by using third-party tools such like trxer. You can type “TrxerConsole.exe TestResultFileName.trx” in cmd to convert it to HTML using trxer.

Tuesday, April 9, 2019

[Solved] Start pointer [line=185, lineOffset=32] should be before end pointer - Exception of sonar

I was using jenkins version - 2.168 and Sonar version - 6.4 with c# plugin sonar-csharp-plugin-5.6.0.577.
When i try to analyze a c# project I got this error and the sonar analisis get failed.

I tried with ;

  1. Changing the sonar c# plugin. (Started from 5.XXX ro 6.XXX).
  2. Disabling some of the rules.(At one point I disable all the rules). But no luck.

So I exclude some of the folders of the project and it works.

So the actual issue is with a one .cs file so I was able to isolate that file and permanently exclude that file form the sonar analysis workspace. 

Tuesday, February 12, 2019

How to fix : Error: Cannot find module 'typescript' - Sonar

Possible solutions:

https://nodejs.org/en/download/ install Node JS.

1st :
Go to the jenkins workspace and type "npm install  typescript"
Then try to run the sonar process.

If still you get the error.
try
Go to the jenkins workspace and type "npm install -g  typescript"