Skip to content
This repository has been archived by the owner on Dec 16, 2022. It is now read-only.

Commit

Permalink
feat: sets the viewport before the goto command. Closes #10
Browse files Browse the repository at this point in the history
  • Loading branch information
tnolet committed Aug 20, 2018
1 parent 0492d96 commit 5fff05e
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/background/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ class RecordingController {
}

chrome.tabs.query({active: true, currentWindow: true}, tabs => {
chrome.tabs.sendMessage(tabs[0].id, { control: 'get-viewport-size' }, response => {
if (response) this.recordCurrentViewportSize(response.value)
})
chrome.tabs.sendMessage(tabs[0].id, { control: 'get-current-url' }, response => {
if (response) this.recordCurrentUrl(response.href)
})
Expand Down Expand Up @@ -93,6 +96,10 @@ class RecordingController {
this.handleMessage({ selector: undefined, value: undefined, action: 'goto*', href })
}

recordCurrentViewportSize (value) {
this.handleMessage({ selector: undefined, value, action: 'viewport*' })
}

handleMessage (msg) {
console.debug('receiving message', msg)
if (msg.control) return this.handleControlMessage(msg)
Expand Down
4 changes: 4 additions & 0 deletions src/content-scripts/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ class EventRecorder {
if (msg.control && msg.control === 'get-current-url') {
resp({ href: window.location.href })
}

if (msg.control && msg.control === 'get-viewport-size') {
resp({ value: { width: window.innerWidth, height: window.innerHeight } })
}
})
const msg = { control: 'event-recorder-started' }
sendMessage(msg)
Expand Down
3 changes: 3 additions & 0 deletions src/popup/code-generator/CodeGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ export default class CodeGenerator {
case 'goto*':
result += ` await page.goto('${href}')\n`
break
case 'viewport*':
result += ` await page.setViewport({ width: ${value.width}, height: ${value.height} })\n`
break
case 'reload':
result += ` await page.reload()\n`
break
Expand Down
8 changes: 7 additions & 1 deletion src/popup/components/RecordingTab.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</div>
<div class="event-description">
<div class="event-action">{{event.action}}</div>
<div class="event-props text-muted">{{event.selector || event.href }}</div>
<div class="event-props text-muted">{{event.selector || parseEventValue(event)}}</div>
</div>
</li>
</ul>
Expand All @@ -29,6 +29,12 @@
props: {
isRecording: { type: Boolean, default: false },
liveEvents: { type: Array, default: () => { return [] } }
},
methods: {
parseEventValue (event) {
if (event.action === 'viewport*') return `width: ${event.value.width}, height: ${event.value.height}`
if (event.action === 'goto*') return event.href
}
}
}
</script>
Expand Down
19 changes: 18 additions & 1 deletion src/popup/components/__tests__/RecordingTab.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,24 @@ describe('RecordingTab.vue', () => {
expect(wrapper.find('.event-list').isEmpty()).toBe(true)
})

test('it has the correct recording events state', () => {
test('it has the correct recording Puppeteer custom events state', () => {
const wrapper = mount(RecordingTab)
wrapper.setProps({
isRecording: true,
liveEvents: [{
action: 'goto*',
href: 'http://example.com'
}, {
action: 'viewport*',
selector: undefined,
value: { width: 1280, height: 800 }
}]
})
expect(wrapper.element).toMatchSnapshot()
expect(wrapper.find('.event-list').isEmpty()).toBe(false)
})

test('it has the correct recording DOM events state', () => {
const wrapper = mount(RecordingTab)
wrapper.setProps({ isRecording: true, liveEvents: [{ action: 'click', selector: '.main > a.link', href: 'http://example.com' }] })
expect(wrapper.element).toMatchSnapshot()
Expand Down
177 changes: 177 additions & 0 deletions src/popup/components/__tests__/__snapshots__/RecordingTab.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,183 @@ exports[`RecordingTab.vue it has the correct pristine / empty state 1`] = `
</div>
`;

exports[`RecordingTab.vue it has the correct recording DOM events state 1`] = `
<div
class="tab recording-tab"
>
<div
class="content"
>
<div
class="empty"
style="display: none;"
>
<img
alt="desert"
src="/images/Desert.svg"
width="78px"
/>
<h3>
No recorded events yet
</h3>
<p
class="text-muted"
>
Click record to begin
</p>
</div>
<div
class="events"
style=""
>
<p
class="text-muted text-center"
style="display: none;"
>
Waiting for events...
</p>
<ul
class="event-list"
>
<li
class="event-list-item"
>
<div
class="event-label"
>
1.
</div>
<div
class="event-description"
>
<div
class="event-action"
>
click
</div>
<div
class="event-props text-muted"
>
.main &gt; a.link
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
`;

exports[`RecordingTab.vue it has the correct recording Puppeter custom events state 1`] = `
<div
class="tab recording-tab"
>
<div
class="content"
>
<div
class="empty"
style="display: none;"
>
<img
alt="desert"
src="/images/Desert.svg"
width="78px"
/>
<h3>
No recorded events yet
</h3>
<p
class="text-muted"
>
Click record to begin
</p>
</div>
<div
class="events"
style=""
>
<p
class="text-muted text-center"
style="display: none;"
>
Waiting for events...
</p>
<ul
class="event-list"
>
<li
class="event-list-item"
>
<div
class="event-label"
>
1.
</div>
<div
class="event-description"
>
<div
class="event-action"
>
goto*
</div>
<div
class="event-props text-muted"
>
http://example.com
</div>
</div>
</li>
<li
class="event-list-item"
>
<div
class="event-label"
>
2.
</div>
<div
class="event-description"
>
<div
class="event-action"
>
viewport*
</div>
<div
class="event-props text-muted"
>
width: 1280, height: 800
</div>
</div>
</li>
</ul>
</div>
</div>
</div>
`;

exports[`RecordingTab.vue it has the correct recording events state 1`] = `
<div
class="tab recording-tab"
Expand Down

0 comments on commit 5fff05e

Please sign in to comment.