<commit_before><?php
// Copyright 2004-present Facebook. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Facebook\WebDriver\Firefox;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\TestPage;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverTestCase;
use PHPUnit\Framework\TestCase;

/**
 * ## Generating/updating the Firefox extension fixture ##
 *
 * For testing purposes, we use dummy Firefox extension (`Fixtures/FirefoxExtension.xpi`), which adds `<div>` element
 * with some text at the end of each page Firefox renders.
 *
 * In case the extension will need to be modified, steps below must be followed,
 * otherwise firefox won't load the modified extension:
 *
 * - Extract the xpi file (it is a zip archive) to some temporary directory
 * - Make needed changes in the files
 * - Install web-ext tool from Mozilla (@see https://github.com/mozilla/web-ext)
 * - Sign in to https://addons.mozilla.org/cs/developers/addon/api/key/ to get your JWT API key and JWT secret
 * - Run `web-ext sign --channel=unlisted --api-key=[you-api-key] --api-secret=[your-api-secret]` in the extension dir
 * - Store the output file (`web-ext-artifacts/[...].xpi`) to the Fixtures/ directory
 *
 * @group exclude-saucelabs
 * @covers \Facebook\WebDriver\Firefox\FirefoxProfile
 */
class FirefoxProfileTest extends TestCase
{
    /** @var FirefoxDriver */
    protected $driver;

    protected $firefoxTestExtensionFilename = __DIR__ . '/Fixtures/FirefoxExtension.xpi';

    protected function setUp(): void
    {
        if (getenv('BROWSER_NAME') !== 'firefox' || empty(getenv('GECKODRIVER_PATH'))
            || WebDriverTestCase::isSauceLabsBuild()) {
            $this->markTestSkipped('The test is run only when running against local firefox');
        }
    }

    protected function tearDown(): void
    {
        if ($this->driver instanceof RemoteWebDriver && $this->driver->getCommandExecutor() !== null) {
            $this->driver->quit();
        }
    }

    public function testShouldStartDriverWithEmptyProfile()
    {
        $firefoxProfile = new FirefoxProfile();
        $this->startFirefoxDriverWithProfile($firefoxProfile);

        $this->driver->get('http://localhost:8000/');
        $element = $this->driver->findElement(WebDriverBy::id('welcome'));
        $this->assertSame(
            'Welcome to the php-webdriver testing page.',
            $element->getText()
        );
    }

    public function testShouldInstallExtension()
    {
        $firefoxProfile = new FirefoxProfile();
        $firefoxProfile->addExtension($this->firefoxTestExtensionFilename);
        $this->startFirefoxDriverWithProfile($firefoxProfile);

        $this->driver->get($this->getTestPageUrl(TestPage::INDEX));

        $this->assertInstanceOf(RemoteWebDriver::class, $this->driver);

        $element = $this->driver->findElement(WebDriverBy::id('webDriverExtensionTest'));
        $this->assertEquals('This element was added by browser extension', $element->getText());
    }

    public function testShouldUseProfilePreferences()
    {
        $firefoxProfile = new FirefoxProfile();

        // Please note, although it is possible to set preferences right into the profile (what this test does),
        // we recommend using the setPreference() method on FirefoxOptions instead, so that you don't need to
        // create FirefoxProfile.
        $firefoxProfile->setPreference('javascript.enabled', false);
        $this->assertSame('false', $firefoxProfile->getPreference('javascript.enabled'));
<commit_msg>Fix firefox extension test flakiness<commit_after><?php
// Copyright 2004-present Facebook. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Facebook\WebDriver\Firefox;

use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\TestPage;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverExpectedCondition;
use Facebook\WebDriver\WebDriverTestCase;
use PHPUnit\Framework\TestCase;

/**
 * ## Generating/updating the Firefox extension fixture ##
 *
 * For testing purposes, we use dummy Firefox extension (`Fixtures/FirefoxExtension.xpi`), which adds `<div>` element
 * with some text at the end of each page Firefox renders.
 *
 * In case the extension will need to be modified, steps below must be followed,
 * otherwise firefox won't load the modified extension:
 *
 * - Extract the xpi file (it is a zip archive) to some temporary directory
 * - Make needed changes in the files
 * - Install web-ext tool from Mozilla (@see https://github.com/mozilla/web-ext)
 * - Sign in to https://addons.mozilla.org/cs/developers/addon/api/key/ to get your JWT API key and JWT secret
 * - Run `web-ext sign --channel=unlisted --api-key=[you-api-key] --api-secret=[your-api-secret]` in the extension dir
 * - Store the output file (`web-ext-artifacts/[...].xpi`) to the Fixtures/ directory
 *
 * @group exclude-saucelabs
 * @covers \Facebook\WebDriver\Firefox\FirefoxProfile
 */
class FirefoxProfileTest extends TestCase
{
    /** @var FirefoxDriver */
    protected $driver;

    protected $firefoxTestExtensionFilename = __DIR__ . '/Fixtures/FirefoxExtension.xpi';

    protected function setUp(): void
    {
        if (getenv('BROWSER_NAME') !== 'firefox' || empty(getenv('GECKODRIVER_PATH'))
            || WebDriverTestCase::isSauceLabsBuild()) {
            $this->markTestSkipped('The test is run only when running against local firefox');
        }
    }

    protected function tearDown(): void
    {
        if ($this->driver instanceof RemoteWebDriver && $this->driver->getCommandExecutor() !== null) {
            $this->driver->quit();
        }
    }

    public function testShouldStartDriverWithEmptyProfile()
    {
        $firefoxProfile = new FirefoxProfile();
        $this->startFirefoxDriverWithProfile($firefoxProfile);

        $this->driver->get('http://localhost:8000/');
        $element = $this->driver->findElement(WebDriverBy::id('welcome'));
        $this->assertSame(
            'Welcome to the php-webdriver testing page.',
            $element->getText()
        );
    }

    public function testShouldInstallExtension()
    {
        $firefoxProfile = new FirefoxProfile();
        $firefoxProfile->addExtension($this->firefoxTestExtensionFilename);
        $this->startFirefoxDriverWithProfile($firefoxProfile);

        $this->driver->get($this->getTestPageUrl(TestPage::INDEX));

        $this->assertInstanceOf(RemoteWebDriver::class, $this->driver);

        // it sometimes takes split of a second for the extension to render the element, so we must use wait
        $element = $this->driver->wait(5, 1)->until(
            WebDriverExpectedCondition::presenceOfElementLocated(WebDriverBy::id('webDriverExtensionTest'))
        );

        $this->assertEquals('This element was added by browser extension', $element->getText());
    }

    public function testShouldUseProfilePreferences()
    {
        $firefoxProfile = new FirefoxProfile();

        // Please note, although it is possible to set preferences right into the profile (what this test does),
        // we recommend using the setPreference() method on FirefoxOptions instead, so that you don't need to
        // create FirefoxProfile.
        $firefoxProfile->setPreference('javascript.enabled', false);
        $this->assertSame('false', $firefoxProfile->getPreference('javascript.enabled'));

{'fraction_non_alphanumeric': 0.11452253093625964, 'fraction_numerical': 0.0030352556619192153, 'mean_word_length': 3.5184599156118144, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 62, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4222138', 'n_tokens_mistral': 2319, 'n_tokens_neox': 2227, 'n_words': 699}