8

When I try an run the code below in visual studio I get the following error : "Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point. "

I've tried finding other entry points but am not that experienced with c# and having trouble with this. To my understanding everything should be correct.

namespace Demos
{
using System;
using System.Drawing;
using Applitools;
using Applitools.Selenium;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

public class HelloWorld2
{
    static string appName = "Hello World 2 App C#";

    // change the value of testName so that it has a unique value on your Eyes system
    static string testName = "Hello World 2 v1";

    // if you have a dedicated Eyes server, set the value of the variable serverURLstr to your URL
    static string serverURLstr = "https://eyesapi.applitools.com";

    //set the value of runAsBatch to true so that the tests run as a single batch
    static Boolean runAsBatch = true;

    // set the value of changeTest to true to introduce changes that Eyes will detect as mismatches
    static Boolean changeTest = true;

    static string weburl = "https://applitools.com/helloworld2";

    public static void Main(string[] args)
    {
        var eyes = new Eyes();
        eyes.ServerUrl = serverURLstr;
        setup(eyes);

        var viewportSizeLandscape = new Size(/*width*/1024, /*height*/ 768);
        var viewportSizePortrait = new Size(/*width*/500, /*height*/ 900);
        IWebDriver innerDriver = new ChromeDriver();

        if (!changeTest) 
        {
            Test01(innerDriver, eyes, viewportSizeLandscape);
            Test01Changed(innerDriver, eyes, viewportSizePortrait);
        } 
        else 
        {
            Test01Changed(innerDriver, eyes, viewportSizeLandscape);
            Test01Changed(innerDriver, eyes, viewportSizePortrait);
        }
        innerDriver.Quit();
    }


    private static void Test01(IWebDriver innerDriver, Eyes eyes, Size viewportSize) 
    {
        // Start the test and set the browser's viewport size
        IWebDriver driver = eyes.Open(innerDriver, appName, testName, viewportSize);
        try 
        {
            driver.Url = weburl;
            eyes.CheckWindow("Before enter name");                 // Visual checkpoint 1

            driver.FindElement(By.Id("name")).SendKeys("My Name"); //enter the name
            eyes.CheckWindow("After enter name");                  // Visual checkpoint 2

            driver.FindElement(By.TagName("button")).Click();      // Click the  button
            eyes.CheckWindow("After Click");                       // Visual checkpoint 3

            Applitools.TestResults result = eyes.Close(false);     //false means don't thow exception for failed tests
            HandleResult(result);
        } 
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        } 
        finally 
        {
            eyes.AbortIfNotClosed();
        }
    }
              

    private static void HandleResult(TestResults result) 
    {
        string resultStr;
        string url = result.Url;
        if (result == null) 
        {
            resultStr = "Test aborted";
            url = "undefined";
        } 
        else 
        {
            url = result.Url;
            int totalSteps = result.Steps;
            if (result.IsNew) 
            {
                resultStr = "New Baseline Created: " + totalSteps + " steps";
            } 
            else if (result.IsPassed) 
            {
                resultStr = "All steps passed:     " + totalSteps + " steps";
            } 
            else 
            {
                resultStr = "Test Failed     :     " + totalSteps + " steps";
                resultStr += " matches=" +  result.Matches;      /*  matched the baseline */
                resultStr += " missing=" + result.Missing;       /* missing in the test*/
                resultStr += " mismatches=" + result.Mismatches; /* did not match the baseline */
            }
        }
        resultStr += "\n" + "results at " + url;
        Console.WriteLine(resultStr);
    }

     private static void Test01Changed(IWebDriver innerDriver, Eyes eyes, Size viewportSize) 
    {
        // Start the test and set the browser's viewport size
        IWebDriver driver = eyes.Open(innerDriver, appName, testName, viewportSize);
        try 
        {
            string webUrlToUse = weburl;
            if (changeTest) 
            {
                webUrlToUse += "?diff2";
            } 
            driver.Url = webUrlToUse;
            if (!changeTest) 
            {  
                eyes.CheckWindow("Before enter name");             // Visual checkpoint 1
            }
            driver.FindElement(By.Id("name")).SendKeys("My Name"); //enter the name
            eyes.CheckWindow("After enter name");                  // Visual checkpoint 2

            driver.FindElement(By.TagName("button")).Click();      // Click the  button
            eyes.CheckWindow("After click");                       // Visual checkpoint 3

            if (changeTest) 
            {  
                eyes.CheckWindow("After click again");             // Visual checkpoint 3
            }
            TestResults result = eyes.Close(false); //false means don't thow exception for failed tests
            HandleResult(result);
        } 
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        } 
        finally 
        {
            eyes.AbortIfNotClosed();
        }
    }

    private static void setup(Eyes eyes) 
    {
        eyes.ApiKey = Environment.GetEnvironmentVariable("APPLITOOLS_API_KEY");
        if (runAsBatch)
        {
            var batchInfo = new Applitools.BatchInfo("Hello World 2 Batch");
            eyes.Batch = batchInfo;
        }
        //eliminate artifacts caused by a blinking cursor - on by default in latest SDK
        eyes.IgnoreCaret = true;
    }
}
}
Pang
  • 9,564
  • 146
  • 81
  • 122
md22
  • 81
  • 1
  • 1
  • 3
  • 4
    Did you have a `Program.cs` class in root of your project with another `public static void Main(string[] args)` method? Also `namespace Demos` should be after all using and before `public class HelloWorld2` – Lemm Jul 31 '19 at 16:03
  • 2
    Another class in your project also contains the Main() method – felix-b Jul 31 '19 at 16:15
  • 1
    Sometimes, when I encounter this error, deleting the /bin and /obj folder of the project solves this issue. – German Casares Jul 31 '19 at 16:23
  • 1
    @Lemm - Actually, `using` statements can be either inside or outside of the `namespace` declaration and there can be subtle reasons for doing so. See [this link](https://stackoverflow.com/a/151560/70104) – Chris Dunaway Jul 31 '19 at 20:08
  • @Lemm No I don't – md22 Aug 01 '19 at 10:13
  • I had this error because I had a file with another Main named Program2.cs in a subdirectory that I had forgotten about. Do a recursive grep for "Main" in the project directory. The error message could be improved by telling which files provide the conflicting Main declarations. – Gabriel Devillers Apr 22 '20 at 12:18

3 Answers3

16

Try adding this to your .csproj project file:

<PropertyGroup>
    <GenerateProgramFile>false</GenerateProgramFile>
</PropertyGroup>
Pang
  • 9,564
  • 146
  • 81
  • 122
Thijs
  • 161
  • 2
  • 6
  • 1
    Good Answer! Thank you – HOÀNG LONG Jan 30 '21 at 02:58
  • 3
    Thank you, @Thijs I had to close down visual studio and restarted then did a Clean Solution then Rebuild Solution to find error no longer show with Rebuild All...succeeded. – Tien Feb 16 '21 at 01:32
13

This can happen if you add a reference to Microsoft.NET.Test.Sdk.

So, if it is just console app and not a test project, remove the reference to Microsoft.NET.Test.Sdk.

James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78
  • I did this because I wanted my test in the same project as the code, kinda lame. How can I pick which entrypoint to use? – justin.m.chase Feb 22 '22 at 22:51
  • @justin.m.chase - As stated in error message, 'Compile with /main to specify the type that contains the entry point.'. So, it seems like you can use this switch to specify which one to use. As I don't have this problem any more, I never investigated the switch. Review the documentation, which is usually quite helpful. – James John McGuire 'Jahmic' Feb 25 '22 at 15:24
0

Please check the startup object in the properties of the project. This issue occurs if we are not able to start the project having multiple entry point.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 06 '22 at 16:32