user1014351
user1014351

Reputation: 447

Is it possible to let chrome devtools give different values of battery level and charging?

I am debugging a javascript that i cannot modify and does different things according battery level and charging status on mobile devices. I work with latest chrome version on android phone connected to a PC using an USB cable and chrome developer tools on PC. My problem is that with this configuration with a script like this:

navigator.getBattery().then(function(battery) {
    var level = battery.level;
    console.log(battery);
});

i always have battery.charghing=true and battery.level=1 while i need to see the script behaviour using a lower battery level and battery.charging=false. Is it possible to do it? I cannot use debugger breakpoint because in that case the script has even different behaviour.

Upvotes: 1

Views: 966

Answers (1)

Sebastian Speitel
Sebastian Speitel

Reputation: 7336

You can always hardcode your debug values or in this case create a complete copy of the original battery:

navigator.getBattery().then(function(battery) {
  var level = battery.level;

  console.log("Original battery:", battery);

  //DEBUG START
  battery = {
    "charging": false,
    "chargingTime": 100,
    "dischargingTime": Infinity,
    "level": 42,
  };
  //DEBUG END

  console.log("Debug battery:", battery);
});

Upvotes: 1

Related Questions