using LoginPI.Engine.ScriptBase; using System; public class YouTubeVideoStatsForNerds : ScriptBase { private void Execute() { /* Metadata: -Utilizing Windows 10 Professional x86; winver 1909; OS Build 18363.476 -ScriptingToolSet ver. 3.6.23 -Scripting date: V1.0: Dec 22 2019 -Crafted using: -Google Chrome Version 79.0.3945.88 (64-bit) -youtube.com */ /* Read me; items to note; caveats: -This script was designed to: -Open a defined web browser -Navigate to youtube.com -Search for and open a video page -Secondary-click the video -Select the "Stats for nerds" option -Take a screenshot of the video with the statistics overlay -Save the screenshot file to a defined path -Please read and set the variables section -Find and use the path to the target application, such as: " -force-renderer-accessibility " , which will open the Chrome application with the graphical objects exposed Replace this into the YouTubeVideoStatsForNerds .app file Example: "C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -force-renderer-accessibility **Some of the code blocks will need to be customized** 1) Depending on YouTube preferences the "target" video might or might not automatically play. If it doesn't automatically play this script will need to be customized to click on the Play button to start the video // Use the Application XRay tool to determine this. Read up more about the Application XRay tool's usage, if need be, in Login PI's documentation 2) The variable for the xPath YouTubeSearchResultToLoad might need to be updated depending on web browser being used, or the YouTube search result desired to be loaded 3) The variable WebBrowserLoadedObject might need to be updated to reflect an object that should only be present once the web browser has initially loaded and is interactive 4) The variable VideoFrame might need to be updated with the proper control data to identify; this statement ensures the video player frame has loaded */ ///////////////////// Starting of variables to define ///////////////////// string InvokeYouTubeVideoScreenshotStatement = @"C:\temp\ScriptingToolSet\Tools\LoginPI.ScriptRunner.StandAlone.exe script=C:\temp\ScriptingToolSet\MyScripts\YouTubeVideoScreenshot\YouTubeVideoScreenshot.cs app=C:\temp\ScriptingToolSet\MyScripts\YouTubeVideoScreenshot\YouTubeVideoScreenshot.app"; // Put the path here that will run the statement which will run the YouTubeVideoScreenshot.cs script. The format of the statement is: " script= app= " // Example: "C:\temp\ScriptingToolSet\Tools\LoginPI.ScriptRunner.StandAlone.exe script=C:\temp\ScriptingToolSet\MyScripts\YouTubeVideoScreenshot\YouTubeVideoScreenshot.cs app=C:\temp\ScriptingToolSet\MyScripts\YouTubeVideoScreenshot\YouTubeVideoScreenshot.app" // To learn about Login PI's ScriptingToolset usage, please refer to the Login PI documentation on Login VSI's website string ProcessNameTaskkill = "chrome*"; // To ensure the target application isn't running before the START(); metafunction, put the process name of the target application here; example: "chrome*" int GlobalTimeoutInSeconds = 60; // Define how long it should take maximum for operations to complete, such as opening a website in Chrome, in seconds. This script will fail, as designed, if this value is surpassed; example: 60 int WaitSeconds = 1; // Define here how many seconds to wait in between functions; example: 2. This will allow for the target application's UI to catch up int CharactersPerMinuteToType = 999; // Define how many characters per minute the typing function will type; example; 180 string WebBrowserProcessName = "chrome"; // Put the process name here for the web browser, example: " chrome " string KeystrokesToClearOutAddressField = "{CTRL+a}{BACK}"; // Put the keystrokes combination to select all text and clear out a field; example "{CTRL+a}{BACK}" -- which will do ctrl+a -> backspace string WhatToSearchOnYouTube = "Login VSI"; // Put the string here to search for on youtube.com, such as " Login VSI " ///////////////////// Ending of variables to define ///////////////////// // Starting the script here... // Ensuring the target application isn't already running then starting the target browser ShellExecute(@"cmd /c taskkill /f /im " + ProcessNameTaskkill,waitForProcessEnd:true,timeout:GlobalTimeoutInSeconds); StartTimer(name:"WebBrowserFullyLoaded"); START(processName:WebBrowserProcessName,timeout:GlobalTimeoutInSeconds); // Making sure the target web browser has loaded by looking from a control that should only be present once the UI is interactive. Replace the WebBrowserLoadedObject statement with the appropriate web browser control data, if need be var WebBrowserLoadedObject = MainWindow.FindControl(className : "Document:Chrome_RenderWidgetHostHWND", title : "Chrome Legacy Window",timeout:GlobalTimeoutInSeconds); StopTimer(name:"WebBrowserFullyLoaded"); MainWindow.Focus(); Wait(WaitSeconds); // This will navigate to youtube.com, wait for the search bar to appear, search for the defined text, and open the defined youtube video result (based on the YouTubeSearchResultToLoad variable definition) KeyDown(KeyCode.ALT); Type("d"); KeyUp(KeyCode.ALT); Type(KeystrokesToClearOutAddressField,cpm:CharactersPerMinuteToType); Wait(WaitSeconds); Type(@"https://www.youtube.com/{enter}",cpm:CharactersPerMinuteToType); StartTimer(name:"YouTubeLoaded"); var SearchField = MainWindow.FindControl(className : "Edit", title : "Search",timeout:GlobalTimeoutInSeconds); StopTimer(name:"YouTubeLoaded"); Wait(WaitSeconds); SearchField.Click(); Type(WhatToSearchOnYouTube,cpm:CharactersPerMinuteToType); Type("{enter}",cpm:CharactersPerMinuteToType); Wait(WaitSeconds); var YouTubeSearchResultToLoad = MainWindow.FindControlWithXPath(xPath : "Document:Chrome_RenderWidgetHostHWND/Group[2]/Hyperlink[3]/Text",timeout:GlobalTimeoutInSeconds); Wait(WaitSeconds); YouTubeSearchResultToLoad.Click(); // This will click on the first result in the YouTube search. This will need to be modified in order to click on a different result, or if a different web browser is being used StartTimer(name:"YouTubeVideoHasLoaded"); // This will verify the YouTube video has loaded var VideoFrame = MainWindow.FindControlWithXPath(xPath : "Document:Chrome_RenderWidgetHostHWND/Group[1]/Group/Unknown",timeout:GlobalTimeoutInSeconds); // This VideoFrame variable is to locate the frame for the YouTube video player in order to determine the video's been loaded and is interactive. It might need to be updated with the proper control data to identify it StopTimer(name:"YouTubeVideoHasLoaded"); // This will secondary-click the video player frame, select the "Stats for nerds" button, verify the "Stats for nerds" frame opens, and takes a screenshot of the YouTube window, saving the screenshot file to a defined location, then closing the Google Chrome application VideoFrame.Focus(); Wait(WaitSeconds); KeyDown(KeyCode.SHIFT); Type("{f10}",cpm:CharactersPerMinuteToType); KeyUp(KeyCode.SHIFT); Wait(WaitSeconds); MainWindow.FindControl(className : "Text", title : "Stats for nerds",timeout:GlobalTimeoutInSeconds).Click(); MainWindow.FindControl(className : "Text", title : "Video ID / sCPN",timeout:GlobalTimeoutInSeconds); Wait(WaitSeconds); ShellExecute(InvokeYouTubeVideoScreenshotStatement,waitForProcessEnd:true,timeout:GlobalTimeoutInSeconds); Wait(WaitSeconds); STOP(); } }