public void StartScript(string scriptText)
        {
            using (_SeleniumExecutionEngine = new SeleniumExecution.SeleniumExecutionEngine(RunOptions.WebDriverPath, new SeleniumExecutionEngineOptions()
            {
                DisableWebSecurity = RunOptions.DisableWebSecurity, RunHeadlessBrowser = RunOptions.NoBrowser
            }))
            {
                _CrawlLangEngine = new CrawlLangEngine(scriptText, _SeleniumExecutionEngine);
                _CrawlLangEngine.ParseScript();
                if (_CrawlLangEngine.HasErrors)
                {
                    ErrorMessages = _CrawlLangEngine.Errors;
                }
                else
                {
                    ExecutionPlan plan = _CrawlLangEngine.GenerateExecutionPlan();
                    try
                    {
                        _SeleniumExecutionEngine.StartEngine();

                        IsRunning = true;
                        OutputSingleton.ClearAllOutputters();
                        if (!string.IsNullOrWhiteSpace(RunOptions.OutputFilePath))
                        {
                            CreepyCrawly.Output.OutputSingleton.CreateFileTextOutputter(RunOptions.OutputFilePath);
                        }
                        if (!string.IsNullOrWhiteSpace(RunOptions.ImageOutputDirectory))
                        {
                            CreepyCrawly.Output.OutputSingleton.CreateImageFileOutputter(RunOptions.ImageOutputDirectory);
                        }
                        CreepyCrawly.Output.OutputSingleton.CreateStringOutputter();
                        CreepyCrawly.Output.OutputSingleton.AssignEventHandlerToStringOutputters(_OutputEventHandler);
                        if (_SeleniumExecutionEngine.IsEngineOk)
                        {
                            plan.Commands.ForEach(cmd =>
                            {
                                cmd.Execute();
                            });
                        }
                        else
                        {
                            CreepyCrawly.Output.OutputSingleton.WriteToStringOutputters("Engine wasn't started :(\nMaybe you are missing an appropriate chromedriver in the app root directory.");
                        }
                    }
                    catch (Exception e)
                    {
                        if (IsRunning)
                        {
                            CreepyCrawly.Output.OutputSingleton.WriteToStringOutputters(string.Format("An error occurred during script execution with message:\n{0}\nSee the following stacktrace:\n{1}", e.Message, e.StackTrace));
                        }
                    }
                }
            }
        }
Beispiel #2
0
        private static void Run(Options options)
        {
            string scriptText = "";

            try
            {
                scriptText = System.IO.File.ReadAllText(options.InputFilePath);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine("An error occurred during opening of file:\n{0}\nSee the following stacktrace:\n{1}", e.Message, e.StackTrace);
                CloseApp();
            }
            using (SeleniumExecutionEngine executionEngine = new SeleniumExecutionEngine(options.ChromeDriverPath, CreateEngineOptions(options)))
            {
                CrawlLangEngine crawlLangEngine = new CrawlLangEngine(scriptText, executionEngine);
                crawlLangEngine.ParseScript();
                if (crawlLangEngine.HasErrors)
                {
                    Console.Error.WriteLine("Found {0} error(s)!", crawlLangEngine.Errors.Count.ToString());
                    crawlLangEngine.Errors.ForEach(err => Console.Error.WriteLine(err));
                }
                else
                {
                    Console.WriteLine("Script seems OK, generating plan!");
                    ExecutionPlan executionPlan = crawlLangEngine.GenerateExecutionPlan();
                    if (executionPlan == null)
                    {
                        Console.Error.WriteLine("Could not generate an execution plan :(");
                    }
                    else
                    {
                        try
                        {
                            Console.WriteLine("Starting engine!");
                            executionEngine.StartEngine();
                            if (options.WriteToStdout)
                            {
                                CreepyCrawly.Output.OutputSingleton.CreateConsoleTextOutputter();
                            }
                            if (Uri.IsWellFormedUriString(options.ResultFilePath, UriKind.RelativeOrAbsolute))
                            {
                                CreepyCrawly.Output.OutputSingleton.CreateFileTextOutputter(options.ResultFilePath);
                            }
                            if (Uri.IsWellFormedUriString(options.ImageDirectoryPath, UriKind.RelativeOrAbsolute))
                            {
                                CreepyCrawly.Output.OutputSingleton.CreateImageFileOutputter(options.ImageDirectoryPath);
                            }
                            //CreepyCrawly.Output.OutputSingleton.CreateStringOutputter();
                            //CreepyCrawly.Output.OutputSingleton.AssignEventHandlerToStringOutputters(__NewOutputAppeared);
                            Console.WriteLine("Starting script execution!");
                            if (executionEngine.IsEngineOk)
                            {
                                executionPlan.Commands.ForEach(cmd =>
                                {
                                    cmd.Execute();
                                });
                            }
                            else
                            {
                                Console.Error.WriteLine("Engine wasn't started :(\nMaybe you are missing an appropriate chromedriver in the app root directory.");
                            }
                        }
                        catch (Exception e)
                        {
                            Console.Error.WriteLine("An error occurred during script execution with message:\n{0}\nSee the following stacktrace:\n{1}", e.Message, e.StackTrace);
                        }
                    }
                }
            }
            CloseApp();
        }