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

Fix conflict with Content Security Policy headers #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ With this we will:
Finally, because it’s hard for us to do automatically, you will need to add the following before any closing `</head>` tags in any of the files in `app/views/layouts`:

```ruby+erb
<%= render 'magic_test/support' if Rails.env.test? %>
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The condition is already inside partial

<%= render 'magic_test/support' %>
```

You can also pass attributes, like CSP nonce, for the `script` tag inside:

```ruby+erb
<%= render 'magic_test/support', nonce: true %>
```

You should be done now! To review what we’ve done for you, be sure to do a `git diff` at this point and make sure our generators didn’t break anything!
Expand Down
48 changes: 0 additions & 48 deletions app/views/magic_test/_context_menu.html

This file was deleted.

46 changes: 46 additions & 0 deletions app/views/magic_test/_context_menu.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
function ready(fn) {
if (document.readyState !== 'loading'){
fn();
} else {
document.addEventListener('DOMContentLoaded', fn);
}
}

ready(function() {
enableKeyboardShortcuts();
});


function enableKeyboardShortcuts() {
function keydown(event) {
if (event.ctrlKey && event.shiftKey && event.key === 'A') {
event.preventDefault();
generateAssertion();
}
}

document.addEventListener('keydown', keydown, false);

function generateAssertion() {
var text = selectedText().trim();
if (text.length > 0) {
var action = "assert page.has_content? '" + text.replace("'", "\\\'") + "'";
var testingOutput = JSON.parse(sessionStorage.getItem("testingOutput"));
var target = "";
var options = "";
testingOutput.push({action: action, target: target, options: options});
sessionStorage.setItem("testingOutput", JSON.stringify(testingOutput));
alert("Generated an assertion for \"" + selectedText() + "\". Type `flush` in the debugger console to add it to your test file.");
}
}

function selectedText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
}
54 changes: 0 additions & 54 deletions app/views/magic_test/_finders.html

This file was deleted.

49 changes: 49 additions & 0 deletions app/views/magic_test/_finders.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
function getPathTo(element) {
if (element.tagName === 'HTML') {
return '/HTML[1]';
}
if (element===document.body) {
return '/HTML[1]/BODY[1]';
}
var ix = 0;
var siblings = element.parentNode.childNodes;
for (var i= 0; i<siblings.length; i++) {
var sibling= siblings[i];
if (sibling===element) {
return getPathTo(element.parentNode)+'/'+element.tagName+'['+(ix+1)+']';
}
if (sibling.nodeType===1 && sibling.tagName===element.tagName) {
ix++;
}
}
}

function visibleFilter(target){
var computedStyle = window.getComputedStyle(target);
return !(computedStyle.display === 'none' || computedStyle.visibility === 'hidden' || target.hidden)
}


function finderForElement(element) {
// Try to find just using the element tagName
var tagName = element.tagName.toLowerCase();
if (document.querySelectorAll(tagName).length === 1) {
return `find('${tagName}')`;
}
// Try adding in the classes of the element
var classList = [].slice.apply(element.classList)
var classString = classList.length ? "." + classList.join('.') : "";
if (classList.length && document.querySelectorAll(tagName + classList).length === 1) {
return `find('${tagName}${classString}')`;
}
// Try adding in the text of the element
var text = element.textContent.trim();
var targets = Array.from(document.querySelectorAll(`${tagName}${classString}`))
.filter(visibleFilter)
.filter((el) => el.textContent.includes(text))
if (text && targets.length === 1) {
return `find('${tagName}${classString}', text: '${text}')`;
}
// use the xpath to the element
return `find(:xpath, '${getPathTo(element)}')`;
}
133 changes: 0 additions & 133 deletions app/views/magic_test/_javascript_helpers.html

This file was deleted.

Loading