Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.x] Add assertScript() #821

Merged
merged 1 commit into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -868,4 +868,23 @@ public function vueAttribute($componentSelector, $key)
"return document.querySelector('".$fullSelector."').__vue__.".$key
);
}

/**
* Assert that the given JavaScript expression has a given value.
*
* @param string $expression
* @param mixed $expected
* @return $this
*/
public function assertScript($expression, $expected = true)
{
$expression = Str::start($expression, 'return ');

PHPUnit::assertEquals(
$expected, $this->driver->executeScript($expression),
"JavaScript expression [{$expression}] mismatched."
);

return $this;
}
}
31 changes: 31 additions & 0 deletions tests/MakesAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,4 +389,35 @@ public function test_assert_vue_contains_with_no_result()
);
}
}

public function test_assert_script()
{
$driver = m::mock(stdClass::class);
$driver->shouldReceive('executeScript')->withArgs(['return 1==1'])->andReturn(true);
$driver->shouldReceive('executeScript')->withArgs(['return 1==1'])->andReturn(true);
$driver->shouldReceive('executeScript')->withArgs(['return 1==2'])->andReturn(false);
$driver->shouldReceive('executeScript')->withArgs(["return 'some string'"])->andReturn('some string');

$resolver = m::mock(stdClass::class);

$browser = new Browser($driver, $resolver);

$browser->assertScript('return 1==1');
$browser->assertScript('1==1');
$browser->assertScript("'some string'", 'some string');

try {
$browser->assertScript('1==2');
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString(
'JavaScript expression [return 1==2] mismatched.',
$e->getMessage()
);
$this->assertStringContainsString(
'Failed asserting that false matches expected true.',
$e->getMessage()
);
}
}
}