botcity-dev/botcity-framework-core

browse anonymous tab or window

csorodrigo opened this issue · 2 comments

Hello,
I search on the api but i didnt find.
Is there anyway to open a anonymous tab or window of the current browser?

Best regards

Hi @csorodrigo,

There are two ways to do this:

WebBot:
If you are using a Web automation, you can set the "--incognito" flag through the default options of the browser being used.

More details here: https://documentation.botcity.dev/frameworks/web/configuration/#__tabbed_3_2

The code would look like this (using Chrome):

// imports
...
import org.openqa.selenium.chrome.ChromeOptions;
import dev.botcity.framework.web.browsers.ChromeConfig;
import dev.botcity.framework.web.browsers.PageLoadStrategy;

...

public void action(BotExecution botExecution) {

        try {
            // Configure whether to run on headless mode
            setHeadless(false);

            // Uncomment to change the default Browser to Firefox
            // setBrowser(Browser.FIREFOX);

            // Uncomment to set the WebDriver path
            setDriverPath("chromedriver.exe");
            
            ChromeConfig chromeConfig = new ChromeConfig();
            ChromeOptions defOptions = (ChromeOptions) chromeConfig.defaultOptions(
                    isHeadless(), // Setting headless mode (using default)
                    getDownloadPath(), // Setting the download folder path (using default)
                    null, // Informing null here will generate a temporary directory
                    PageLoadStrategy.NORMAL // Setting the page load strategy
            );
            
            defOptions.addArguments("--incognito");
            setOptions(defOptions);

            // Opens the BotCity website
            browse("https://botcity.dev");
            wait(5000);
...

DesktopBot:
If you are using a Desktop automation, you can open the browser and then run a keyboard shortcut to open an incognito tab.

For example, using the shortcut CTRL + SHIFT + N:

...
import java.awt.event.KeyEvent;
...

public void action(BotExecution botExecution) {

		try {
			browse("https://botcity.dev");
			wait(2000);
			typeKeys(KeyEvent.VK_CONTROL, KeyEvent.VK_SHIFT, KeyEvent.VK_N);
...

Thank you! It worked, best regards